[Git] - Commit Message规范

Commit Message格式

1
2
3
4
5
<type>(<scope>): <subject>
// 空一行
<body>
// 空一行
<footer>

Header部分只有一行,包括三个字段:

  • type(必需)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 主要type
feat: 增加新功能
fix: 修复bug

# 特殊type
docs: 只改动了文档相关的内容
style: 不影响代码含义的改动,例如去掉空格、改变缩进、增删分号
build: 构造工具的或者外部依赖的改动,例如webpack,npm
refactor: 代码重构时使用
perf: 优化相关,比如提升性能、体验
revert: 执行git revert打印的message
sync: 同步主线或分支的Bug
merge: 代码合并

# 暂不使用type
test: 添加测试或者修改现有测试
perf: 提高性能的改动
ci: 与CI(持续集成服务)有关的改动
chore: 不修改src或者test的其余修改,例如构建过程或辅助工具的变动
  • scope(可选)
    • 用于说明commit影响的范围
    • 格式为项目名/模块名
  • subject(必需)
    • commit目的的简短描述

Body

Body 部分是对本次 commit 的详细描述,可以分成多行。下面是一个范例。

1
2
3
4
5
6
7
More detailed explanatory text, if necessary.  Wrap it to 
about 72 characters or so.

Further paragraphs come after blank lines.

- Bullet points are okay, too
- Use a hanging indent
  • 使用第一人称现在时,比如使用change而不是changed或changes。
  • 应该说明代码变动的动机,以及与以前行为的对比。

Footer 部分只用于两种情况。

  • 不兼容变动

如果当前代码与上一个版本不兼容,则 Footer 部分以BREAKING CHANGE开头,后面是对变动的描述、以及变动理由和迁移方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
BREAKING CHANGE: isolate scope bindings definition has changed.

To migrate the code follow the example below:

Before:

scope: {
myAttr: 'attribute',
}

After:

scope: {
myAttr: '@',
}

The removed `inject` wasn't generaly useful for directives so there should be no code using it.
  • 关闭 Issue
1
Closes #234
1
Closes #123, #245, #992

Revert

还有一种特殊情况,如果当前 commit 用于撤销以前的 commit,则必须以revert:开头,后面跟着被撤销 Commit 的 Header。

1
2
3
revert: feat(pencil): add 'graphiteWidth' option

This reverts commit 667ecc1654a317a13331b17617d973392f415f02.

规范

  • 将subject和body用一空白行分开
  • subject限制在50个字以内
  • subject首字母大写
  • subject结尾不使用标点符号
  • 使用命令语气
  • body部分的长度限制在72字节内
  • 用body来解释what和why

git-commit-plugin

https://marketplace.visualstudio.com/items?itemName=redjue.git-commit-plugin

在vscode里添加git-commit-plugin来辅助

commitizen

1
2
# install
npm install -g commitizen git-cz

然后使用git-cz即可,或者git cz

好的例子

参考