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

相關文章
相關標籤/搜索