优势
- 规范——遵从 AngularJS’s commit message convention
- 简单——通过命令行可视化步骤提示填写对应描述,防止遗漏
- 扩展——支持团队自定义提交模板(cz-customizable)
- 分析——支持生成 commit 日志(standard-version),利于版本迭代
安装
- 项目级别安装(可保证团队成员版本一致)
1
| npm install --save-dev commitizen
|
- 初始化 Commitizen 配套的适配器,如果你的 npm 版本在 5.2 以上,那么你可以使用 npx 来初始化.
1
| npx commitizen init cz-conventional-changelog --save-dev --save-exact
|
该初始化命令主要完成以下 3 步:
- 安装 cz-conventional-changelog 包(提供日志标准)
- 将 cz-conventional-changelog 加入 package.json 的 devDependencies 依赖中
1 2 3 4
| "devDependencies": { "cz-conventional-changelog": "^3.3.0" "cz-customizable": "^6.3.0", },
|
- 在 package.json 根路径新增 npm 执行命令 config.commit 及 config.commitizen 配置项
1 2 3 4 5 6 7 8
| "scripts": { "commit": "cz" }, "config": { "commitizen": { "path": "./node_modules/cz-conventional-changelog" } }
|
husky
- 初始化 husky 配置
1 2
| cd ${项目根目录} npx husky install
|
- 添加 pre-commit 以及 commit-msg hook
commitlint 规范具体请查看 git 规范-提交与合并
1 2 3
| npx husky add .husky/pre-commit "npm run lint && npm run styleLint && git add ." npx husky add .husky/commit-msg "npx --no-install commitlint --edit \$1"
|
- 添加 package scripts
1 2 3
| # 往 package.json scripts 字段中添加下面的内容 "lint": "eslint --ext .vue,.js,.ts,.tsx ./ --max-warnings 0 --fix", "styleLint": "stylelint **/*.{css,scss,sass,less,vue} --fix"
|