跳到主要内容

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工作流改进
ciCI/CD 配置相关
types类型定义修改
wip正在进行中的工作,开发中的提交(不建议合入主分支)

Commit 辅助工具

为了减少手写提交信息的出错率,可以使用交互式工具生成符合规范的 Commit Message

Commitizen

Commitizen 是一个提交信息生成器(CLI 工具框架),可以通过问答方式引导你填写 typescopesubject 等信息,避免格式不统一。

它本身是“外壳”,真正决定提问内容和输出格式的是适配器(adapter)。

cz-conventional-changelog

cz-conventional-changelogCommitizen 最常见的适配器之一,基于 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 工具方案),支持更灵活的交互配置和更强的可定制能力,比如:

  • 自定义问题顺序、默认值、校验规则
  • 更友好的交互体验(提示信息、颜色、高级配置)
  • commitlinthusky 等工具组合更顺滑

说明:

  • cz-git 是 Commitizen Adapter,走 git cz 流程时通常需要 commitizen
  • czg 是作者提供的独立 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

参考文档