Git 提交信息格式指南

Table of Contents

不知道你有没有这样的感觉,不管是 SVN 还是 Git,提交代码的时候 commit 格式总是不好拿捏,不知道应该以什么样的格式提交才「算好」。 今天看到一篇文章,总结了 Git 提交信息指南,觉得还不错。因为是英文的,就翻译了一下,意译非直译。

译者注:Udacity 好像是一家国际化的培训教育公司。


标题:Udacity Git Commit Message Style Guide

原文链接:https://udacity.github.io/git-styleguide/

<2022-10-20 Thu 17:31> 基于如下内容更新(更加实用些,不再是上面链接的翻译):

  1. https://www.freecodecamp.org/news/how-to-write-better-git-commit-messages/
  2. https://cbea.ms/git-commit/

1. 消息格式(Message Structure)

提交消息由用空行分割的三部分组成:标题、可选的 body 、可选的 footer。布局看起来像这样:

type: subject

body

footer

标题由消息类型和主题两部分组成。

2. 类型(The Type)

类型是标题的一部分,可是是下面的几种类型之一:

  • build 构建相关、外部依赖变更
  • ci 持续集成依赖
  • docs 文档完善
  • feat 新特性
  • fix Bug 修复
  • perf 性能优化
  • project 工程化相关,git 忽略文件、开发环境配置等
  • refactor 生产代码重构
  • revert 代码回退
  • style 格式化,缺少冒号,等等,没有代码的修改
  • test: 添加测试用例,重构测试代码,没有生产代码的修改

3. 主题(The Body)

主题不应该超过 50 个字符,以大写字母单词开始,不要以句号结束。

精简的描述提交做的事情,而不是做了什么。比如使用 change ,而不是 changed 或者 changes

4. 内容(The Body)

不是所有的提交都需要写丰富的 body,只有提交内容需要解释和有上下文的时候才需要写,所以是可选的。使用 body 是用来解释提交的 whatwhy ,而不是 how

当写 body 的时候,在标题和 body 之间留一个空行,每行不能超过 72 个字符。

4.1. 其他书写规范

  1. 标题行首字母大写
  2. 标题最多 50 个字符
  3. 不要用句号结束标题行
  4. 段落最多 72 个字符
  5. 用空行分割段落

5. 页脚(The Footer)

footer 是可选的,用来写 issue 的跟踪 IDs。

6. 提交消息举例

feat: Summarize changes in around 50 characters or less

More detailed explanatory text, if necessary. Wrap it to about 72
characters or so. In some contexts, the first line is treated as the
subject of the commit and the rest of the text as the body. The
blank line separating the summary from the body is critical (unless
you omit the body entirely); various tools like `log`, `shortlog`
and `rebase` can get confused if you run the two together.

Explain the problem that this commit is solving. Focus on why you
are making this change as opposed to how (the code explains that).
Are there side effects or other unintuitive consequenses of this
change? Here's the place to explain them.

Further paragraphs come after blank lines.

 - Bullet points are okay, too

 - Typically a hyphen or asterisk is used for the bullet, preceded
   by a single space, with blank lines in between, but conventions
   vary here

If you use an issue tracker, put references to them at the bottom,
like this:

Resolves: #123
See also: #456, #789

First created: 2018-07-09 20:35:44
Last updated: 2022-12-11 Sun 12:49
Power by Emacs 27.1 (Org mode 9.4.4)