Git的提交消息规范
对于 git 提交规范 来说,不同的团队可能会有不同的标准,那么今天就以目前使用较多的 Angular团队规范 延伸出的 Conventional Commits specification(约定式提交)来配置。
规范模板:
<type>[optional scope]: <description>
[optional body]
[optional footer(s)]
-------- 翻译 -------------
<类型>[可选 范围]: <描述>
[可选 正文]
[可选 脚注]
配置:
-
全局下载commitizen包:
npm install -g commitizen -
在项目下安装:
npm install cz-customizable -
添加以下配置到
package.json中:... "config": { "commitizen": { "path": "node_modules/cz-customizable" } } -
根目录下创建
.cz-config.js用于自定义提示文件:module.exports = { // 可选类型 types: [ { value: 'feat', name: 'feat: 新功能' }, { value: 'fix', name: 'fix: 修复' }, { value: 'docs', name: 'docs: 文档变更' }, { value: 'style', name: 'style: 代码格式(不影响代码运行的变动)' }, { value: 'refactor', name: 'refactor: 重构(既不是增加feature,也不是修复bug)' }, { value: 'perf', name: 'perf: 性能优化' }, { value: 'test', name: 'test: 增加测试' }, { value: 'chore', name: 'chore: 构建过程或辅助工具的变动' }, { value: 'revert', name: 'revert: 回退' }, { value: 'build', name: 'build: 打包' } ], // 消息步骤 messages: { type: '请选择提交类型:', customScope: '请输入修改范围(可选):', subject: '请简要描述提交(必填):', body: '请输入详细描述(可选):', footer: '请输入要关闭的issue(可选):', confirmCommit: '确认使用以上信息提交?(y/n/e/h)' }, // 跳过问题 skipQuestions: ['body', 'footer'], // subject文字长度默认是72 subjectLimit: 72 } -
使用
git cz代替git commit然后跟着命令行操作就好了。
注意事项:
如果在package.json文件中设置了"type" : "module"即它会默认将js文件识别为esm类型的,但这个cz-customizable包又是需要使用commonjs类型的,此时会报错:
# 报错信息:
require() of ES Module C:....cz-config.js from C:...\node_modules\find-config\src\find-config.js not supported.
.cz-config.js is treated as an ES module
file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead rename .cz-config.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in C:...\package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).
可行方案:在package.json文件中设置
"config": {
"commitizen": { // not needed for standlone usage
"path": "node_modules/cz-customizable"
},
"cz-customizable": {
"config": "config/path/to/my/config.js" // 如果未找到配置,它将在您的主目录中查找.cz-config.mjs 或 .config/cz-config.cjs
}
}
因为是commonjs类型且package.json中设置了type: module为esm类型,所以我设置为commit.cjs然后内容和之前.cz-config.js一样。
总结:
关于git提交规范,Angular团队提出的Conventional Commits规范是当前使用较广泛的一种。该规范提倡以"类型(scope):描述信息"的格式撰写提交信息,其中类型表示本次提交的类型,比如feat(新增功能)、fix(修复bug)等;scope表示影响的范围;描述信息则详细描述本次提交的内容。该规范最大的好处是提供了一致性,使得查看提交历史更清晰直观。但是也有不同团队根据自己的需求进行修改或调整,所以并非完全严格遵守。在我自己的一些小项目中我也会尽可能地去规范化提交。更多好处在于使用合理的提交规范可以很大程度增强协作效率和项目可维护性。但也需要根据实际情况灵活调整,找到适合自己团队的标准。
参考文档:
leoforfree/cz-customizable:一个独立的提交消息帮助程序或可定制的commitizen适配器,用于 https://github.com/commitizen/cz-cli