commitizen规范commit
在多人协作的项目中,如果Git
提交说明过于混乱,是不利于代码review的。如果Git
提交说明足够精准、详细,不仅有利于代码的review也有利于后期排查问题,甚至我们可以根据commit
信息生成响应日志。
本文主要介绍通过cz工具,以及附和Angular
规范的提交说明来规范commit
信息。也可以查看vuejs,他的提交说明就是根据Angular
规范来的。
本文示例: cz-example
配置
1 | npm install -g commitizen |
创建commitlint.config.js
1 | module.exports = { |
修改package.json
1 | "scripts": { |
提交流程
git cz
Select the type of change that you’re committing
- feat: A new feature
- fix: A bug fix
- docs: Documentation only changes
- style: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
- refactor: A code change that neither fixes a bug nor adds a feature
- perf: A code change that improves performance
- test: Adding missing tests or correcting existing tests
What is the scope of this change (e.g. component or file name): (press enter to skip) 填写所修改的组件、文件名。可跳过
Write a short, imperative tense description of the change 进行一个简短的描述 max 94
Provide a longer description of the change 进行一个详细的描述可跳过
Are there any breaking changes? 是否发生重大改变 可跳过
- A BREAKING CHANGE commit requires a body. Please enter a longer description of the commit itself:重大改变的描述
git pull
git push
在merge到master之前需要生成CHANGELOG,之后合并最近的两个commit即可merge
具体解释
规范的Git提交说明
- 提供更多的历史信息,方便快速浏览
- 可以过滤某些
commit
,便于筛选代码review
- 可以追踪
commit
生成更新日志 - 可以关联issues
Git
提交说明结构
Git
提交说明可分为三个部分:Header、Body和Footer。
1 | <Header> <Body> <Footer> |
Header
Header部分包括三个字段type(必需)、scope(可选)和subject(必需)。
1 | <type>(<scope>): <subject>ja |
type
type用于说明 commit
的提交性质。
值 | 描述 |
---|---|
feat | 新增一个功能 |
fix | 修复一个bug |
docs | 文档变更 |
style | 代码格式(不影响功能,例如空格、分号等格式修正) |
refactor | 代码重构 |
perf | 改善性能 |
test | 测试 |
build | 变更项目构建或外部依赖(例如scopes:webpack 、gulp 、npm 等) |
ci | 更改持续集成软件的配置文件和package 中的scripts 命令,例如scopes:Travis、Circle等 |
chore | 变更构建流程活辅助工具 |
revert | 代码回退 |
scope
scope说明commit
影响的范围。scope依据项目而定,例如在业务项目中可以依据菜单或者功能模块划分,如果是组件库开发,则可以依据组件划分。
subject
subject是commit
的简短描述。
Body
commit
的详细描述,说明代码提交的详细说明。
Footer
如果代码的提交是不兼容变更或关闭缺陷,则Footer必需,否则可以省略。
不兼容变更
当前代码与上一个版本不兼容,则Footer以BREAKING CHANGE开头,后面是对变动的描述、以及变动的理由和迁移方法。
关闭缺陷
如果当前提交是针对特定的issue,那么可以在Footer部分填写需要关闭的单个 issue 或一系列issues。
Commitizen
commitizen/cz-cli是一个可以实现规范的提交说明的工具:
When you commit with Commitizen, you’ll be prompted to fill out any required commit fields at commit time. No more waiting until later for a git commit hook to run and reject your commit (though that can still be helpful). No more digging through CONTRIBUTING.md to find what the preferred format is. Get instant feedback on your commit message formatting and be prompted for required fields.
提供选择的提交信息类别,快速生成提交说明。安装cz工具:
1 | npm install -g commitizen |
Commitizen适配器
cz-conventional-changelog
如果需要在项目中使用commitizen生成符合AngularJS规范的提交说明,初始化cz-conventional-changelog适配器:
1 | commitizen init cz-conventional-changelog --save --save-exact |
初始化命令主要进行了3件事情
- 在项目中安装cz-conventional-changelog 适配器依赖
- 将适配器依赖保存到
package.json
的devDependencies
字段信息 - 在
package.json
中新增config.commitizen
字段信息,主要用于配置cz工具的适配器路径:
1 | "devDependencies": { |
Commitizen日志
如果使用了cz工具集,配套conventional-changelog可以快速生成开发日志:
1 | npm install conventional-changelog conventional-changelog-cli -D |
在pacage.json
中加入生成日志命令:
1 | "version": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0 && git add CHANGELOG.md" |
You could follow the following workflow
- Make changes
- Commit those changes
- Pull all the tags
- Run the npm version [patch|minor|major] command
- Push
执行npm run version
后可查看生产的日志CHANGELOG.md。