前言
本文基于
“@commitlint/cli”: “^17.3.0”“@commitlint/config-conventional”: “^17.3.0”“husky”: “^8.0.2”
commit message规范
主流:angular 规范,目前使用最广的写法,比较合理和系统化,并且有配套的工具。
1.安装依赖
npm install @commitlint/cli @commitlint/config-conventional --devnpm install husky --dev
or
yarn add @commitlint/cli @commitlint/config-conventional --devyarn add husky --dev
2.在项目根目录下创建并配置 .commitlintrc.js
// feat: 新增功能// fix: 修复bug// perf: 性能优化(提高代码性能的改变)// reverts: 代码回退// refactor: 重构(即不是新增功能,也不是修改bug的代码变动)// chore: 对非 src 和 test 目录的修改(日常事务)// docs: 更新文档// style: 格式化(不影响代码运行的变动,空格、备注等)// test: 增加/修改测试// ci: 对CI配置文件和脚本的更改// build: 影响构建系统或外部依赖项的更改(maven、gradle、npm等)module.exports = {extends: ['@commitlint/config-conventional'],rules: {'type-enum': [2,'always',['feat', 'fix', 'docs', 'style', 'chore', 'perf', 'reverts', 'refactor', 'build']],'subject-full-stop': [0, 'never'],'subject-case': [0, 'never']}};
3.修改 package.json 中 “scripts” 字段
"scripts": {"prepare": "husky install"}
4.生成 .husky 文件夹
npm run prepare
or
yarn prepare
此时在当前项目的根目录下会生成一个.husky文件夹
5.生成 commit-msg 文件
npx husky add .husky/commit-msg
or
yarn husky add .husky/commit-msg
在当前项目的.husky文件夹中创建了一个commit-msg文件
6.修改 commit-msg 文件
将原先的undefined修改成如下
npx --no-install commitlint --edit "$1"
7.测试
不符合规范的commit报错,终止提交
git commit -m'测试提交'
commit符合规范(规范查看第二步备注)
创建 commit 成功
如果本篇文章对你有帮助的话,很高兴能够帮助上你。
当然,如果你觉得文章有什么让你觉得不合理、或者有更简单的实现方法又或者有理解不来的地方,希望你在看到之后能够在评论里指出来,我会在看到之后尽快的回复你。