Git Commit Message 规范

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

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

1. 规范 Commit Msg 的做用

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

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

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

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

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

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

3) 能够直接从 commit 生成 Change Log

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

2. Angular 规范的 Commit Msg 语法

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

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

其中,Header 是必需的, Body 和 Footer 能够省略。app

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 个标示。google

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

相关文章
相关标签/搜索