Git進階:如何提交標準化的Git Commit Message

Git進階:如何提交標準化的Git Commit Message

如果你對git還不熟或者不瞭解,請移步 ,我等你回來,這裡是進階篇

Git 每次提交代碼,都要寫 Commit message(提交說明),否則就不允許提交。

git commit -m "hello world"

上面代碼的-m參數,就是用來指定 commit mesage 的。

如果一行不夠,可以只執行git commit,就會跳出文本編輯器,讓你寫多行。

git commit

基本上,你寫什麼都行,就像下面這樣

Git進階:如何提交標準化的Git Commit Message

但是,一般來說,commit message 應該清晰明瞭,說明本次提交的目的。

Git進階:如何提交標準化的Git Commit Message

從上面兩張圖中,我想你已經能看出一些端倪來了吧,相較而言哪種更加能促進開發不言自明。目前,社區有多種 Commit message 的寫法規範。本文介紹Angular 規範(見上圖),這是目前使用最廣的寫法,比較合理和系統化,並且有配套的工具。

一、Commit message 的作用

格式化的Commit message,有幾個好處。

(1)提供更多的歷史信息,方便快速瀏覽。

比如,下面的命令顯示上次發佈後的變動,每個commit佔據一行。你只看行首,就知道某次 commit 的目的。

git log <last> HEAD --pretty=format:%s
/<last>
Git進階:如何提交標準化的Git Commit Message

(2)可以過濾某些commit(比如文檔改動),便於快速查找信息。

比如,下面的命令僅僅顯示本次發佈新增加的功能。

git log <last> HEAD --grep feature
/<last>

(3)可以直接從commit生成Change log。

Change Log 是發佈新版本時,用來說明與上一個版本差異的文檔,詳見後文。

Git進階:如何提交標準化的Git Commit Message

二、Commit message 的格式

每次提交,Commit message 都包括三個部分:Header,Body 和 Footer。

<type>(<scope>): <subject>
// 空一行

// 空一行
<footer>
/<footer>/<subject>/<scope>/<type>

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

不管是哪一個部分,任何一行都不得超過72個字符(或100個字符)。這是為了避免自動換行影響美觀。

2.1 Header

Header部分只有一行,包括三個字段:type(必需)、scope(可選)和subject(必需)。

(1)type

type用於說明 commit 的類別,只允許使用下面7個標識。

  • feat:新功能(feature)
  • fix:修補bug
  • docs:文檔(documentation)
  • style: 格式(不影響代碼運行的變動)
  • refactor:重構(即不是新增功能,也不是修改bug的代碼變動)
  • test:增加測試
  • chore:構建過程或輔助工具的變動

如果type為feat和fix,則該 commit 將肯定出現在 Change log 之中。其他情況(docs、chore、style、refactor、test)由你決定,要不要放入 Change log,建議是不要。

(2)scope

scope用於說明 commit 影響的範圍,比如數據層、控制層、視圖層等等,視項目不同而不同。

(3)subject

subject是 commit 目的的簡短描述,不超過50個字符。

  • 以動詞開頭,使用第一人稱現在時,比如change,而不是changed或changes
  • 第一個字母小寫
  • 結尾不加句號(.)

2.2 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)使用第一人稱現在時,比如使用change而不是changed或changes。

(2)應該說明代碼變動的動機,以及與以前行為的對比。

2.3 Footer(可省略)

Footer 部分只用於兩種情況。

(1)不兼容變動

如果當前代碼與上一個版本不兼容,則 Footer 部分以BREAKING CHANGE開頭,後面是對變動的描述、以及變動理由和遷移方法。

BREAKING CHANGE: isolate scope bindings definition has changed.
To migrate the code follow the example below:
Before:
scope: {
myAttr: 'attribute',
}
After:
scope: {

myAttr: '@',
}
The removed `inject` wasn't generaly useful for directives so there should be no code using it.

(2)關閉 Issue

如果當前 commit 針對某個issue,那麼可以在 Footer 部分關閉這個 issue 。

Closes #234

也可以一次關閉多個 issue 。

Closes #123, #245, #992

2.4 Revert(可省略)

還有一種特殊情況,如果當前 commit 用於撤銷以前的 commit,則必須以revert:開頭,後面跟著被撤銷 Commit 的 Header。

revert: feat(pencil): add 'graphiteWidth' option
This reverts commit 667ecc1654a317a13331b17617d973392f415f02.

Body部分的格式是固定的,必須寫成This reverts commit <hash>.,其中的hash是被撤銷 commit 的 SHA 標識符。/<hash>

如果當前 commit 與被撤銷的 commit,在同一個發佈(release)裡面,那麼它們都不會出現在 Change log 裡面。如果兩者在不同的發佈,那麼當前 commit,會出現在 Change log 的Reverts小標題下面。

三、Commitizen

Commitizen是一個撰寫合格 Commit message 的工具。

安裝命令如下(mac下面需要管理員權限,sudo ...)。

 npm install -g commitizen

安裝changelog,是生成changelog的工具

npm install -g conventional-changelog
npm install -g conventional-changelog-cli

執行

npm ls -g -depth=0

檢驗上面兩個工具是否安裝成功,得到結果如下,表示成功:然後,在項目目錄裡,運行下面的命令,使其支持 Angular 的 Commit message 格式。

/usr/local/lib
├── [email protected]
├── [email protected]
├── [email protected]
└── [email protected]

然後,運行下面命令,使其支持Angular的Commit message格式。

commitizen init cz-conventional-changelog --save --save-exact

但是注意,因為commitizen工具是基於Node.js的,而我們iOS項目工程目錄下是沒有package.json文件,所以會報錯:(以下部分作為非node環境下的配置,不關心者可以跳過,直接進入下一部分“使用”教程

npm WARN saveError ENOENT: no such file or directory, open '/Users/Elite/package.json'
npm WARN enoent ENOENT: no such file or directory, open '/Users/Elite/package.json'

對於此種錯誤,創建一個空的package.json文件,然後進入到項目目錄,執行

npm init --yes

會生成項目對應項目的package.json,將項目目錄下產生的package.json的內容寫入到自己建的package.json(/User/Elite/package.json)中,如果有多個項目,將各項目生成的package.json內容寫入到package.json中,下面是我的配置(/User/Elite/package.json)

[{
"name": "salary",
"version": "1.0.0",
"description": "> v1.0 涵蓋所有老師(非中教)的基本工資、獎勵工資、懲罰工資。",
"main": "index.js",
"scripts": {
"test": "echo \\"Error: no test specified\\" && exit 1"
},
"repository": {
"type": "git",
"url": "git@***.***.com:erp/salary.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"cz-conventional-changelog": "^2.1.0"
},
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
},
"dependencies": {}
},
{

"name": "ats",
"version": "1.0.0",
"description": "composer.json composer配置 vendor 第三方類庫",
"main": "index.js",
"directories": {
"test": "tests"
},
"scripts": {
"test": "echo \\"Error: no test specified\\" && exit 1"
},
"repository": {
"type": "git",
"url": "git@***.***.com:ats/ats.git"
},
"keywords": [],
"author": "",
"license": "ISC"
}]

然後進入到你要操作的項目目錄,執行

conventional-changelog -p angular -i CHANGELOG.md -s

此時項目中多了CHANGELOG.md文件,表示生成 Change log成功了。以後,凡是用到git commit 命令的時候統一改為git cz,然後就會出現選項,生成符合格式的Commit Message。實例如下:

? Select the type of change that you're committing: (Use arrow keys)
? feat: A new feature
fix: A bug fix
docs: Documentation only changes
style: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
refactor: A code change that neither fixes a bug nor adds a feature
perf: A code change that improves performance
test: Adding missing tests or correcting existing tests

然後按操作執行,即可產生change log。如果最後產生一個這樣的錯誤:

Error: Could not resolve /Users/Elite/web/node_modules/cz-conventional-changelog.  

Cannot find module '/Users/Elite/web/node_modules/cz-conventional-changelog'

只需做個軟連接即可:

ln -s /Users/Elite/node_modules /Users/Elite/web/node_modules

使用

Git進階:如何提交標準化的Git Commit Message

在代碼更改後,提交commit message的時候,不再使用git commit -m方法,而是git cz,將會出現交互式選項,讓你選擇或者輸入信息,給你一個完善的commit message。示例動圖:

Git進階:如何提交標準化的Git Commit Message

不用以為到了上面就完了,下面還有關於校驗commit的配置

Git進階:如何提交標準化的Git Commit Message


四、validate-commit-msg

validate-commit-msg 用於檢查 Node 項目的 Commit message 是否符合格式。

它的安裝是手動的。首先,拷貝下面這個JS文件,放入你的代碼庫。文件名可以取為validate-commit-msg.js。

接著,把這個腳本加入 Git 的 hook。下面是在package.json裡面使用 ghooks,把這個腳本加為commit-msg時運行。

 "config": {
"ghooks": {
"commit-msg": "./validate-commit-msg.js"
}
}

然後,每次git commit的時候,這個腳本就會自動檢查 Commit message 是否合格。如果不合格,就會報錯。

$ git add -A 
$ git commit -m "edit markdown"
INVALID COMMIT MSG: does not match "<type>(<scope>): <subject>" ! was: edit markdown
/<subject>/<scope>/<type>

五、生成 Change log

如果你的所有 Commit 都符合 Angular 格式,那麼發佈新版本時, Change log 就可以用腳本自動生成(例1,例2,例3)。

生成的文檔包括以下三個部分。

  • New features
  • Bug fixes
  • Breaking changes.

每個部分都會羅列相關的 commit ,並且有指向這些 commit 的鏈接。當然,生成的文檔允許手動修改,所以發佈前,你還可以添加其他內容。

conventional-changelog 就是生成 Change log 的工具,運行下面的命令即可。

$ npm install -g conventional-changelog
$ cd my-project
$ conventional-changelog -p angular -i CHANGELOG.md -w

上面命令不會覆蓋以前的 Change log,只會在CHANGELOG.md的頭部加上自從上次發佈以來的變動。

如果你想生成所有發佈的 Change log,要改為運行下面的命令。

$ conventional-changelog -p angular -i CHANGELOG.md -w -r 0

為了方便使用,可以將其寫入package.json的scripts字段。

{
"scripts": {
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -w -r 0"
}
}

以後,直接運行下面的命令即可生成像開始幾張圖片一樣的開發記錄了。

$ npm run changelog


以上配置,送給還不知道的你,我也會在接下來的vue組件庫中使用這個規範來開發。工欲善其事必先利其器!!


鏈接文章

https://www.ruanyifeng.com/blog/2016/01/commit_message_change_log.html

https://www.jianshu.com/p/d264f88d13a4

https://github.com/commitizen/cz-cli/issues/289


分享到:


相關文章: