跳转至

Git Commit规范化

概要: 基于Angular git commit style,写出清晰易懂的git commit message

创建时间: 2022.05.19 01:09:46

更新时间: 2022.10.10 21:22:48

基本格式

Bash
1
2
3
4
5
<type>(<scope>): <subject>
<空行>
<body>
<空行>
<footer>
三部分组成,即head标题,body主体和footer备注

  • type: 提交修改的类型
  • feat: new feature, 新功能
  • fix: fix bug, 修复故障
  • docs: update documentation, 更新文档
  • style: code style, 代码格式优化,如删除多余的空格空行
  • refactor: code refactor, 代码重构,无增删功能和故障修复
  • perf: improve performance, 提升代码性能
  • test: add or correct testcase, 增加或纠正测试用例
  • ci: CI工具方法或配置的变动
  • scope: 提交修改的影响范围/模块,如UI, DB, API, UT, FT, ST, module-A, module-B等
  • subject: 简述本次改动的内容(英语字符50个以内,汉字20个以内),结尾无标点符号.或者

body部分

Explain the motivation for the change in the commit message body. This commit message should explain why you are making the change. You can include a comparison of the previous behavior with the new behavior in order to illustrate the impact of the change.

主体部分主要解释提交此变更的具体动机和原因,可以增加与修改前的对比来凸显变更的必要性(英语需要使用一般现在时进行表述)

The footer can contain information about breaking changes and deprecations and is also the place to reference GitHub issues, Jira tickets, and other PRs that this commit closes or is related to.

备注/结语部分主要用来:

  1. 描述对项目造成的重大影响
  2. 描述是否有类/方法/变量已废弃
  3. 描述是否关闭了某个issue
  4. 描述是否合入了某个PR或者MR

有重大变更...

Text Only
1
2
3
4
5
6
BREAKING CHANGE: <breaking change summary>
<BLANK LINE>
<breaking change description + migration instructions>
<BLANK LINE>
<BLANK LINE>
Fixes #<issue number>

废弃了某个接口/类/方法...

Text Only
1
2
3
4
5
6
DEPRECATED: <what is deprecated>
<BLANK LINE>
<deprecation description + recommended update path>
<BLANK LINE>
<BLANK LINE>
Closes #<pr number>

代码回退场景

默认使用git revert ${commit-id}命令生成的格式即可,示例如下

Text Only
1
2
3
revert: feat(UI): add 'pause' button

This reverts commit c99d84140286cf13aef4d2c093d020e36c6cf20a.

提交信息示例

Angular项目扒取的一个commit如下

Text Only
1
2
3
4
5
docs(common): url link format updated ([#44645](https://github.com/angular/angular/pull/44645))

fix the url link format from http to https in BAZEL.md && component-styles.md

PR Close [#44645](https://github.com/angular/angular/pull/44645)

commit格式化工具

基于macOS平台,使用commitizen工具来规范自己的git commit,其它平台请参考Commitizen by commitizen

安装commitizen

使用brew安装commitizen如下

Bash
brew install commitizen

使用commitizen

执行git cz或者cz --help查看commitizen的使用帮助,下面是一些高频命令。

使用自定义的git commit规范

Bash
cz --name ${config_name}

初始化项目git commit规范

Bash
cd ${project_path}
cz init


按照提示完成配置后,即可生成对应的项目cz配置文件.cz.yaml

YAML
1
2
3
4
commitizen:
  name: cz_conventional_commits
  tag_format: $version
  version: 0.0.

使用cz编写commit

git add file1 file2 ...后,执行cz c或者git cz c即可进入交互式终端

最后即可生成符合规范的commit

使用cz生成changelog

执行cz ch或者git cz ch即可自动生成CHANGELOG.md文件(此操作将会覆盖旧的changelog)

其它cz命令

  • cz -n ${cz_template}: 使用特定名称的cz规范模版
  • cz ls: 显示当前
  • cz example: 给出一个符合cz规范的commit示例
  • cz info: 告诉你cz是什么
  • cz schema: 显示当提交规范的格式
  • cz check -m ${commit_msg}: 检查此条commit message是否符合cz规范
  • cz version: 显示cz当前版本

参考

  1. Angular git commit style
  2. Angular project
  3. Commit message 和 Change log 编写指南 - 阮一峰的网络日志
  4. Commitizen by commitizen