Git Commit 规范
提示
不规范的 Git Commit 信息给代码更新追踪溯源增加了麻烦,编写清晰、有意义的 Git 提交信息对于项目的维护和协作至关重要
这里收集一些常用的 Git Commit 模板参考,必要时可以使用一些工具来 约束 Git Commit 信息
Git Commit 模板
Git Commit 信息通常包含以下几个部分
<type>(<scope>): <subject>
其中:
type: 描述提交类型scope: 描述提交范围,可选subject: 提交的简短描述
type 常用类型如下表所示
| 类型 | 描述 |
|---|---|
| feat | 新增功能 |
| fix | 修复 bug |
| docs | 文档或注释修改 |
| style | 代码样式调整(不影响逻辑) |
| refactor | 代码重构 |
| perf | 性能优化 |
| revert | 回滚,撤销更改 |
| test | 添加或修改测试 |
| chore | 构建流程、依赖管理等杂项更改 |
| workflow | 工作流改进 |
| ci | CI/CD 配置相关 |
| types | 类型定义修改 |
| wip | 正在进行中的工作,开发中的提交(不建议合入主分支) |
Commit 辅助工具
为了减少手写提交信息的出错率,可以使用交互式工具生成符合规范的 Commit Message
Commitizen
Commitizen 是一个提交信息生成器(CLI 工具框架),可以通过问答方式引导你填写 type、scope、subject 等信息,避免格式不统一。
它本身是“外壳”,真正决定提问内容和输出格式的是适配器(adapter)。
cz-conventional-changelog
cz-conventional-changelog 是 Commitizen 最常见的适配器之一,基于 Conventional Commits 规范,开箱即用,适合快速落地团队规范。
Install
npm install -g commitizen cz-conventional-changelog
echo '{ "path": "cz-conventional-changelog" }' > ~/.czrc
Usage
~ git cz
cz-cli@4.3.1, cz-conventional-changelog@3.3.0
? Select the type of change that you're committing: feat: A new feature
? 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 chars):
(4) test
? Provide a longer description of the change: (press enter to skip)
? Are there any breaking changes? No
? Does this change affect any open issues? No
[dev dfd99df] feat: test
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test.yaml
cz-git
cz-git 是新一代的 Commitizen 适配器(也可以理解为更现代的交互式 commit 工具方案),支持更灵活的交互配置和更强的可定制能力,比如:
- 自定义问题顺序、默认值、校验规则
- 更友好的交互体验(提示信息、颜色、高级配置)
- 与
commitlint、husky等工具组合更顺滑
说明:
cz-git是 Commitizen Adapter,走git cz流程时通常需要commitizenczg是作者提供的独立 CLI,可理解为内置了cz-git适配能力的替代方案,不强依赖commitizen
Install
npm install -g cz-git commitizen
echo '{ "path": "cz-git", "$schema": "https://raw.githubusercontent.com/Zhengqbbb/cz-git/refs/tags/v1.12.0/docs/public/schema/cz-git.json" }' > ~/.czrc
Usage
~ git cz
cz-cli@4.3.1, cz-git@1.12.0
? Select the type of change that you're committing: feat: A new feature
? Denote the SCOPE of this change (optional): custom
? Denote the SCOPE of this change:
? Write a SHORT, IMPERATIVE tense description of the change:
[Infinity more chars allowed]
测试
? Provide a LONGER description of the change (optional). Use "|" to break new line:
? List any BREAKING CHANGES (optional). Use "|" to break new line:
? Select the ISSUES type of change (optional): skip
###--------------------------------------------------------###
feat: 测试
###--------------------------------------------------------###
? Are you sure you want to proceed with the commit above? Yes
[dev 5d1fc77] feat: 测试
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test.yaml