git 提交规范配置.md
1. 安装commitizen
pnpm i -D commitizen
2. 安装提交时要使用的规则
pnpm add cz-conventional-changelog
3. package.json中增加如下配置,不在此文件配置请查看第11项
{ ..., "config": { "commitizen": { "path": "./node_modules/cz-conventional-changelog", "types": { "feat": { "description": "新功能(feature)", "title": "新功能" }, "fix": { "description": "修复bug", "title": "bug修改" }, "docs": { "description": "文档(documentation)", "title": "" }, "style": { "description": "格式化代码" }, "refactor": { "description": "代码重构(既不是新增功能,也不是修复bug的代码改动)" }, "test": { "description": "增加测试" }, "chore": { "description": "构建过程或辅助工具变动" }, "build": { "description": "构建系统或者包依赖更新" }, "ci": { "description": "CI,CD配置和脚本文件更新" }, "revert": { "description": "版本回退" } } } }, ... }
4. 和husky配合在提交git信息时校验
npx husky add .husky/prepare-commit-msg "exec < /dev/tty && node_modules/.bin/cz --hook || true"
5. 在编写提交时,Commitizen 为我们提供了更好的体验,但是如果我们使用命令 git commit -m "message" 而不遵循格式规范,会自动 ctrl + c 它将隐藏提示并保存提交这不是我想要的,为了避免这种情况,我们将使用 commitlint 安装 commitlint 和要使用的约定
pnpm add @commitlint/cli @commitlint/config-conventional -D
6. 项目根目录下创建commitlint.config.js配置文件
module.exports = { extends: ['@commitlint/config-conventional'] }
7. 配合husky完成配置
pnpx husky add .husky/commit-msg "npx --no-install commitlint --edit \"$1\""
8. 提供钩子
pnpx --no-install commitlint --edit
9. 在package.json中增加scripts
{ ..., scripts: { ..., "commit": "git add . && cz", ... } ... }
10. 命令提示
| 命令 | 解释 |
|---|---|
| feat | 新功能(feature) |
| fix | 修补bug |
| docs | 文档(documentation) |
| style | 格式化代码(不影响代码运行的变动) |
| refactor | 重构(即不是新增功能,也不是修改bug的代码变动) |
| test | 增加测试 |
| chore | 构建过程或辅助工具变动 |
| build | 构建系统或者包依赖更新 |
| ci | ci配置和脚本文件更新 |
| revert | commit 回退 |
11. 不在package.json中配置,创建.cz.json文件
{ "path": "./node_modules/cz-conventional-changelog", "types": { "feat": { "description": "新功能(feature)", "title": "新功能" }, "fix": { "description": "修复bug", "title": "bug修改" }, "docs": { "description": "文档(documentation)", "title": "" }, "style": { "description": "格式化代码" }, "refactor": { "description": "代码重构(既不是新增功能,也不是修复bug的代码改动)" }, "test": { "description": "增加测试" }, "chore": { "description": "构建过程或辅助工具变动" }, "build": { "description": "构建系统或者包依赖更新" }, "ci": { "description": "CI,CD配置和脚本文件更新" }, "revert": { "description": "版本回退" } } }
浙公网安备 33010602011771号