一、前言
Git 每次提交代碼,都要寫 Commit message(提交說明),否則就不允許提交。基本上寫什么都行,但是一般來說,commit message 應該清晰明了,說明本次提交的目的,但是二、Commit message的作用
-
提供更多的歷史信息,方便快速瀏覽
比如,下面的命令顯示上次發布后的變動,每個commit占據一行。你只看行首,就知道某次 commit 的目的。
git log <last tag> HEAD --pretty=format:%s
-
可以過濾某些commit,便於篩選代碼review
比如,下面的命令僅僅顯示本次發布新增加的功能。
$ git log <last release> HEAD --grep feature
-
可以追蹤commit生成更新日志
-
可以關聯issues
三、Git提交說明結構
Git提交說明可分為三個部分:Header、Body和Footer。
<type>(<scope>): <subject>
// 空一行
<body>
// 空一行
<footer>
Header
Header部分包括三個字段type(必需)、scope(可選)和subject(必需)。
<type>(<scope>): <subject>
Vue源碼的提交說明省略了
scope。
1、type
type用於說明 commit 的提交性質。
| 值 | 描述 |
|---|---|
| feat | 新增一個功能 |
| fix | 修復一個Bug |
| docs | 文檔變更 |
| style | 代碼格式(不影響功能,例如空格、分號等格式修正) |
| refactor | 代碼重構 |
| perf | 改善性能 |
| test | 測試 |
| build | 變更項目構建或外部依賴(例如scopes: webpack、gulp、npm等) |
| ci | 更改持續集成軟件的配置文件和package中的scripts命令,例如scopes: Travis, Circle等 |
| chore | 變更構建流程或輔助工具 |
| revert | 代碼回退 |
2、scope
scope說明commit影響的范圍。scope依據項目而定,例如在業務項目中可以依據菜單或者功能模塊划分,如果是組件庫開發,則可以依據組件划分。
提示:
scope可以省略。
3、subject
subject是commit的簡短描述。
Body
是對本次commit的詳細描述,說明代碼提交的詳細說明,可以分成多行。

Footer
Footer 部分只用於兩種情況。
1)不兼容變動
如果當前代碼與上一個版本不兼容,則 Footer 部分以BREAKING CHANGE開頭,后面是對變動的描述、以及變動理由和遷移方法。
2)關閉 Issue
如果當前 commit 針對某個issue,那么可以在 Footer 部分關閉這個 issue 。
Closes #234
也可以一次關閉多個 issue 。
Closes #123, #245, #992
總結:如果代碼的提交是不兼容變更或關閉缺陷,則Footer必需,否則可以省略。
四、Commitizen
commitizen是一個幫助規范commit message的工具
提供選擇的提交信息類別,快速生成提交說明
安裝 commitizen :
npm install -g commitizen
五、Commitizen適配器
1、cz-conventional-changelog
如果需要在項目中使用commitizen生成符合AngularJS規范的提交說明,初始化cz-conventional-changelog適配器
commitizen init cz-conventional-changelog --save-dev --save-exact
如果當前已經有其他適配器被使用,則會報以下錯誤,此時可以加上--force選項進行再次初始化

初始化命令主要進行了3件事情
-
在項目中安裝cz-conventional-changelog 適配器依賴
-
將適配器依賴保存到package.json的devDependencies字段信息
-
在package.json中新增config.commitizen字段信息,主要用於配置cz工具的適配器路徑:
"devDependencies": { "cz-conventional-changelog": "^2.1.0" }, "config": { "commitizen": { "path": "./node_modules/cz-conventional-changelog" } }
通過git cz命令代替git commit命令,從而調出如下圖的界面,根據界面提示輸入 commit 信息並提交。


2、cz-customizable
如果想定制項目的提交說明,可以使用cz-customizable適配器:
安裝適配器:
npm install cz-customizable --save-dev
將之前符合Angular規范的cz-conventional-changelog適配器路徑改成cz-customizable適配器路徑:
"devDependencies": { "cz-customizable": "^5.3.0" }, "config": { "commitizen": { "path": "node_modules/cz-customizable" } }
官方提供了一個.cz-config.js示例文件
'use strict'; module.exports = { types: [ {value: '特性', name: '特性: 一個新的特性'}, {value: '修復', name: '修復: 修復一個Bug'}, {value: '文檔', name: '文檔: 變更的只有文檔'}, {value: '格式', name: '格式: 空格, 分號等格式修復'}, {value: '重構', name: '重構: 代碼重構,注意和特性、修復區分開'}, {value: '性能', name: '性能: 提升性能'}, {value: '測試', name: '測試: 添加一個測試'}, {value: '工具', name: '工具: 開發工具變動(構建、腳手架工具等)'}, {value: '回滾', name: '回滾: 代碼回退'} ], scopes: [ {name: '模塊1'}, {name: '模塊2'}, {name: '模塊3'}, {name: '模塊4'} ], // it needs to match the value for field type. Eg.: 'fix'
/* scopeOverrides: { fix: [ {name: 'merge'}, {name: 'style'}, {name: 'e2eTest'}, {name: 'unitTest'} ] }, */
// override the messages, defaults are as follows
messages: { type: '選擇一種你的提交類型:', scope: '選擇一個scope (可選):', // used if allowCustomScopes is true
customScope: 'Denote the SCOPE of this change:', subject: '短說明:\n', body: '長說明,使用"|"換行(可選):\n', breaking: '非兼容性說明 (可選):\n', footer: '關聯關閉的issue,例如:#31, #34(可選):\n', confirmCommit: '確定提交說明?' }, allowCustomScopes: true, allowBreakingChanges: ['特性', '修復'], // limit subject length
subjectLimit: 100 };
再次使用git cz命令進行提交說明:


更多:Adapters
六、Commitizen校驗
1、commitlint
校驗提交說明是否符合規范,當我們運行 git commmit -m 'xxx' 時,用來檢查 xxx 是否滿足固定格式的工具。安裝校驗工具commitlint:
npm install -g @commitlint/cli
@commitlint/config-conventional
安裝符合Angular風格的校驗規則
npm install -g @commitlint/config-conventional
在項目中新建commitlint.config.js文件,這個文件就是配置文件,可以設置 commitlint 檢查規則。具體規則可以參考這里:
module.exports = { extends: ['@commitlint/config-conventional'] };
module.exports = { extends: ['@commitlint/config-angular'], rules: { 'type-enum': [ 2, 'always', [ 'WIP', // 開發中
'feat', // 新特性
'improvement', // 加強現有特性
'fix', // 修補bug
'refactor', // 重構
'docs', // 文檔
'test', // 單元測試
'config', // 配置文件
'style', // 格式需改
'perf', // 性能提升
'ci', // ci
'revert', // 版本回退
'chore', // 其他修改
], ], 'type-empty': [2, 'never'], // type不能為空
'type-case': [0, 'always', 'lower-case'], // type不限制大小寫
'subject-empty': [2, 'never'], // subject(簡短得描述)不能為空
'subject-max-length': [1, 'always', 50], // subject最大長度,超出只會警告,不阻止提交
'body-leading-blank': [1, 'always'], 'footer-leading-blank': [1, 'always'], 'header-max-length': [2, 'always', 72], } };
rule配置說明::rule由name和配置數組組成,如:'name:[0, 'always', 72]',數組中第一位為level,可選0,1,2,0為 disable,1為warning,2為error,第二位為應用與否,可選always|never,第三位該rule的值。具體配置例子如下:
scope:一個可選的修改范圍。用於標識此次提交主要涉及到代碼中哪個模塊。
description:一句話描述此次提交的主要內容,做到言簡意賅。
husky
husky是一個可以幫我們設置 git hook 的工具,使用它為我們git commit時添加一個hook,提交時就會觸發這個hook,執行 commitlint 自動檢查我們提交的 commit message,如果不符合規范,可以阻止提交
使用husky方法:
npm install husky --save-dev
在package.json中配置git commit提交時的校驗鈎子:
"husky": { "hooks": { "commit-msg": "commitlint -E HUSKY_GIT_PARAMS" } }
這段配置告訴了git hooks,當我們在當前項目中執行 git commit -m '測試提交' 時將觸發commit-msg事件鈎子並通知husky,從而執行 commitlint -E HUSKY_GIT_PARAMS命令,也就是我們剛開始安裝的./node_modules/.bin/commitlint,它將讀取commitlint.config.js配置規則並對我們剛剛提交的測試提交這串文字進行校驗,若校驗不通過,則在終端輸出錯誤,commit終止。
2、validate-commit-msg
除了使用commitlint校驗工具,也可以使用validate-commit-msg校驗工具對cz提交說明是否符合Angular規范進行校驗。
