git flow 流程
下面是项目里面如何加入git 提交流程规范
1. 安装 commitizen
commitzen约束git commit message 的工具
yarn add commitizen cz-conventional-changelog --dev
2. 在package.json中加入命令行工具
"scripts": {
"commit": "git-cz"
},
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
}
3. 和husky一起约束git commit 在commit时候对于message进行检查
- 安装commitlint帮助校验message
yarn add @commitlint/config-conventional @commitlint/cli husky --dev
- 新建.commitlintrc.js
module.exports = {
extends: [
''@commitlint/config-conventional''
],
rules: {
}
};
- 在package.json增加配置husky
"husky": {
"hooks": {
"commit-msg": "commitlint -e $GIT_PARAMS"
}
}
4. 测试 git commit
发现commit msg 没有通过检查
5. 提交时git commit 换为 yarn commit
会出现提示按照提示一步一步写出合乎规范的message
最后得到的commit message如图格式:
6. 发布版本自动生成changelog
- 安装standard-version:自动生成changelog 和 version 的工具
yarn add standard-version --dev
- 加入package script
{
"script": {
"release": "standard-version"
}
- 新建.versionrc.json文件
{
"types": [
{
"type": "feat",
"section": "New Features"
},
{
"type": "fix",
"section": "Bug Fixes"
},
{
"type": "refactor",
"section": "Refactor"
},
{
"type": "style",
"section": "Style"
},
{
"type": "docs",
"section": "Docs"
},
{
"type": "test",
"section": "Other"
},
{
"type": "build",
"section": "Other"
},
{
"type": "chore",
"section": "Other"
},
{
"type": "perf",
"section": "Other"
},
{
"type": "ci",
"section": "Other"
}
]
}
- 最后发布版本的时候
第一次发布 yarn release --first-release 会过滤掉package.json的version字段不改变, 生成changelog,会打tag
以后发布 yarn release 会在package.json 的version字段加1 同时改变changelog,会打tag
这时候会现在一条commit 记录,需要把它和tag一起push上去。