Git Commit 规范及自动校验
目标:对项目开发人员的每次
commit
书写格式进行自动校验
1 规范
1 主要有以下组成
<type>(<scope>): <subject>
// 空一行
<body>
// 空一行
<footer>
- 标题行: 必填, 描述主要修改类型和内容
- 主题内容: 描述为什么修改, 做了什么样的修改, 以及开发的思路等等
- 页脚注释: 放 Breaking Changes 或 Closed Issues
2 具体说明
- scope: commit 影响的范围, 比如: route, component, utils, build...
- subject: commit 的概述
- body: commit 具体修改内容, 可以分为多行
- footer: 一些备注, 通常是 BREAKING CHANGE 或修复的 bug 的链接.
常用的修改项
DEMO
例如本次开发实现了用户登录的功能;则
commit
提交样式如下:
feat(login): add login feature
1.mobile login
2.email login
3.find password
Type: commit 的类型
- build: 影响build system 或者相关依赖(如: gulp, broccoli, npm, yarn)
- ci: 更改 CI 配置文件或者脚本(如:Circle, BrowserStack, SauceLabs)
- docs: 文档修改
- feat: 新特性
- fix: 修改问题
- perf: 性能优化
- refactor: 代码重构
- style: 代码格式修改, 注意不是 css 修改
- test: 测试用例修改
- chore: 其他修改, 比如构建流程, 依赖管理.
可以使用Commitizen
代替 git commit
可以使用cz-cli工具代替 git commit
全局安装,并在项目根目录初始化安装
yarn global add commitizen cz-conventional-changelog
commitizen init cz-conventional-changelog --yarn --dev --exact
then some change in your package.json
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
}
全局安装后使用git cz
代替 git commit
就可以了。
2 Commit 格式自动校验
1 安装commitlint
1.安装工具
npm install --save-dev @commitlint/cli @commitlint/config-angular
2.生成配置文件commitlint.config.js
方法 1 命令行工具生成
echo "module.exports = {extends: ['@commitlint/config-angular']};" > commitlint.config.js
方法 2 手动生成
项目根目录下创建commitlint.config.js
这个文件,并配置
module.exports = { extends: ["@commitlint/config-conventional"] };
2 安装husky
工具
commitlint.js.org/#/guides-lo…
yarn add -D husky
2 配置husky
校验规则
//package.json
"husky": {
"hooks": {
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
}
}
由此就可以实现每次提交
git commit
时候自动校验commit
的书写是否符合规范。