網(wǎng)絡(luò)上很多關(guān)于 commit message 的想法都來源于 tpope搅方,在他看來咱台,一個好的 Git commit messge 應(yīng)該是這樣的:
Capitalized, short (50 chars or less) summary
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 an email 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); tools like rebase can get confused if you run the
two together.
如上:
- 首先是一個不超過 50 個字符的摘要(summary),單獨(dú)占一行,首字母大寫,行尾不要加點(diǎn)
.
。 - 如果有必要進(jìn)行進(jìn)一步解釋卒暂,那么在摘要行下面空一行,添加描述信息娄帖。描述信息每行不超過 72 個字符也祠。
commit message 應(yīng)該使用祈使語氣,也就是 “Fix bug”块茁,而不是 “Fixed bug” 或者 “Fixes bug”齿坷。這是為了與 git merge
和 git revert
自動生成的 commit message 相一致桂肌。
關(guān)于這一行的摘要怎么寫,也可以有一些約定:
Leading active verb | Explanation |
---|---|
Add | Create a capability e.g. feature, test, dependency. |
Cut | Remove a capability e.g. feature, test, dependency. |
Fix | Fix an issue e.g. bug, typo, accident, misstatement. |
Bump | Increase the version of something e.g. dependency. |
Make | Change the build process, or tooling, or infra. |
Start | Begin doing something; e.g. create a feature flag. |
Stop | End doing something; e.g. remove a feature flag. |
Refactor | A code change that MUST be just a refactoring. |
Reformat | Refactor of formatting, e.g. omit whitespace. |
Optimize | Refactor of performance, e.g. speed up code. |
Document | Refactor of documentation, e.g. help files. |
Bullet points are okay, too
Typically a hyphen or asterisk is used for the bullet, followed by a
single space, with blank lines in between, but conventions vary hereUse a hanging indent
在詳細(xì)描述部分永淌,可以用 -
或者 *
分點(diǎn)闡釋夺欲,每點(diǎn)的文字部分與 -
和 *
之間空一格咕缎,同時點(diǎn)與點(diǎn)之間以空行分隔,使用懸掛縮進(jìn)。
具體操作時激蹲,有兩種方式:
- 直接在
git commit
命令上添加參數(shù):
$ git commit -m "Title" -m "Description ......"
- 使用不帶參數(shù)的
git commit
莉撇,在自動打開的編輯器中進(jìn)行操作觉义,默認(rèn)是 Vim测摔。
推薦使用第 2 種方式,比較清楚和直觀所坯。如果在 Vim 中編輯 commit message谆扎,可以在 .vimrc
中加入以下配置, 開啟拼寫檢查和設(shè)置文本寬度為 72 :
autocmd FileType gitcommit setlocal spell textwidth=72
可以考慮在 commit message 中加入相關(guān)的 PR, issue 鏈接,可以是一個編號芹助,比如 #209
, GitHub 會自動關(guān)聯(lián)到對應(yīng) PR 和 issue 的 URL堂湖。在 GitHub 上進(jìn)行 merge 時,它也會自動幫我們加上相關(guān)的 PR 編號状土。
如果涉及本項(xiàng)目之外的 issue无蜂,可以直接寫永久鏈接,避免沖突蒙谓。
引用 issue 時斥季,如果出現(xiàn)一些關(guān)鍵詞,比如 “Close #209”, 那么 GitHub 就會關(guān)閉 issue #209累驮。詳細(xì)內(nèi)容以及一些其他關(guān)鍵詞可參見 Closing issues using keywords.
以上內(nèi)容不用完全照做酣倾,只須做到 “力所能及” 的一些點(diǎn)。另外慰照,每個項(xiàng)目灶挟,每個團(tuán)隊(duì)也都可能有自己的一些規(guī)范,盡量統(tǒng)一即可毒租。
參考:
[1] A Note About Git Commit Messages
[2] How to commit a change with both “message” and “description” from the command line?
[3] 5 Useful Tips For A Better Commit Message
[4] How to write a Git Commit Message (2014)
[5] git_commit_message