Git Commit Message 规范

今天来说说团队开发中,对于 Git commit message 规范问题。

社区上有各种 Commit message 的规范,本文介绍 Angular 规范,目前使用较广,比较合理和系统化,并且有配套的工具。

1. 规范 Commit Msg 的作用

1) 提供更多的历史信息,方便快速浏览

例如,命令显示上次发布后的变动,每个 commit 占据一行。只看首行,就知道某次 commit 的目的。

$ git log <last tag> HEAD --pretty=format:%s
 

2) 可以过滤某些 commit ,便于快速查找信息

例如,下面的命令过滤仅显示本次发布新增加的功能。

$ git log <last release> HEAD --grep feature
 

3) 可以直接从 commit 生成 Change Log

Change Log 是发布新版本时,用来说明与上一版本差异的文档。

2. Angular 规范的 Commit Msg 语法

每一次提交,Commit message 都包括 3 个部分: Header, Body 和 Footer 。

<type>(<scope>): <subject>
<BLANK LINE>
<body>
<BLANK LINE>
<footer>
 

其中,Header 是必需的, Body 和 Footer 可以省略。

Commit Msg 的任何一行都不得超过 100 个字符。这样是为了在 GitHub 以及各种 Git 工具中更容易阅读。

2.1. Message Header

Header 部分只有一行,它包含对更改的简单描述,其中包括 「类型」( type )、「可选范围」( scope ) 和 「主题」( subject )。

//Message Header Demo

fix($compile): couple of unit tests for IE9

style($location): add couple of missing semi colons

feat($compile): simplify isolate scope bindings
 

2.1.1. 类型 type

type 用于说明 commit 的类别,只允许使用一下 7 个标示。

feat:新功能(feature)

fix:修补bug

docs:文档(documentation)

style: 格式(不影响代码运行的变动)

refactor:重构(即不是新增功能,也不是修改bug的代码变动)

test:增加测试

chore:构建过程或辅助工具的变动
 

2.1.2. 影响范围 scope

「范围」可以是指定提交更改位置的任何内容。例如 browser 、 $compile 、 SrootScope 、 ngHref 、 ngClick 、 ngView 等。

如果没有合适的「范围」,可以用 * 。

2.1.3 主题 subject

subject 是 commit 目的的简单描述,不超过 50 个字符。

* 使用祈使句,并且以第一人称现在时。

* 首字母不要大写

* 结尾 不要 加句号(dot)
 

2.2. Message Body

Body 部分是对本次 commit 的详细描述,可以分成多行。

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
 

需要注意两点:

1) 使用第一人称现在时。

2) 应该说明代码变动的动机,以及与以前行为的对比。

2.3. Footer

2.3.1. 重大更改 Breaking Changes

所有的「重大更改」必须在 Footer 作为「重大更改块」被提及到,它应该以 BREAKING CHANGE: 开头,后面跟着一个空格或者两个空行。然后,Commit Msg 的其余部分是对更改,理由和迁移说明的描述。

REAKING CHANGE:


    isolate scope bindings definition has changed and
    the inject option for the directive controller injection was removed.
    
    To migrate the code follow the example below:
    
    Before:
    
    scope: {
      myAttr: 'attribute',
      myBind: 'bind',
      myExpression: 'expression',
      myEval: 'evaluate',
      myAccessor: 'accessor'
    }
    
    After:
    
    scope: {
      myAttr: '@',
      myBind: '@',
      myExpression: '&',
      // myEval - usually not useful, but in cases where the expression is assignable, you can use '='
      myAccessor: '=' // in directive's template change myAccessor() to myAccessor
    }
    
    The removed `inject` wasn't generaly useful for directives so there should be no code using it.
 

2.3.2. 关闭 Issue (Referencing Issues)

如果当前 commit 针对某个 issue ,那么可以在 Footer 部分关闭这个 issue 。Closes 关键字为前缀。

Closes #123
 

也可以一次关闭多个 issue 。

Closes #123, #243, #545
 

2.4. 还原 (Revert)

如果当前的 commit 是还原到之前的 commit,那么当前 Commit msg Header 要以 revert: 开头,并且后面紧跟着还原 Commit 的 Header 。这时 Body 里面要以 This reverts commit <hash> 固定式,其中 <hash> 是 commit 的 SHA 标示符。

revert: feat(pencil): add 'graphiteWidth' option

This reverts commit 667ecc1654a317a13331b17617d973392f415f02.
 

参考文献

Angular 规范: https://docs.google.com/document/d/1QrDFcIiPjSLDn3EL15IJygNPiHORgU1_OOAqWjiDU5Y/edit

阮一峰 博客: http://www.ruanyifeng.com/blog/2016/01/commit_message_change_log.html

posted @ 2019-10-31 14:45  阿萨姆✌️  阅读(806)  评论(0编辑  收藏  举报