[TOP]
最近在學(xué)習(xí)規(guī)范使用git開發(fā),發(fā)現(xiàn)一個比較好用的來規(guī)范comment的工具,記錄一下,一般來說,commit message 應(yīng)該清晰明了摆尝,說明本次提交的目的,所以需要一些規(guī)范來使這些comment變得可讀.commitizen則是最近發(fā)現(xiàn)的一款比較易用的工具
git的提交使用git commit -m "hello world"
來提交comment,但是一些像hello world這樣沒有意義的comment讓人無法理解這次的提交到底是為了什么
下面是一些基礎(chǔ)介紹如果覺得麻煩直接查看第二部分
1. commit message format(信息域)
commit message一般分為三個部分Header多艇,Body 和 Footer
<type>(<scope>): <subject>
// 空一行
<body>
// 空一行
<footer>
其中逻恐,Header 是必需的,Body 和 Footer 可以省略
Example:
PS D:\git\pythonPractice> git log
commit 58a7a966acb9aa2fffc0e02c9ce3be64b8949991 (HEAD -> master)
Author: Zhiwei Tian <hebeitianzhiwei@outlook.com>
Date: Fri Aug 17 17:38:36 2018 +0800
feat(serve): add grpc server
1.1 HEAD
-
type
用于說明commit
的類別峻黍,只允許使用下面7個標(biāo)識
feat:新功能(feature)
fix:修補(bǔ)bug
docs:文檔(documentation)
style: 格式(不影響代碼運(yùn)行的變動)
refactor:重構(gòu)(即不是新增功能复隆,也不是修改bug的代碼變動)
test:增加測試
chore:構(gòu)建過程或輔助工具的變動
-
scope
用來說明本次Commit影響的范圍,即簡要說明修改會涉及的部分,比如數(shù)據(jù)層姆涩、控制層挽拂、視圖層等, -
subject
comment所在的位置,這次提交的簡短描述
1.2 Body 是對本次 commit 的詳細(xì)描述,可以分成多行
1.3 Footer 部分只用于兩種情況
- 不兼容變動
如果當(dāng)前代碼與上一個版本不兼容骨饿,則
Footer
部分以BREAKING CHANGE
開頭亏栈,后面是對變動的描述、以及變動理由和遷移方法
- 關(guān)閉 Issue
如果當(dāng)前 commit 針對某個issue样刷,那么可以在 Footer 部分關(guān)閉這個 issue (可依次關(guān)閉過個issue
Closes #123, #245, #992
)
1.4 Revert
還有一種特殊情況仑扑,如果當(dāng)前 commit 用于撤銷以前的 commit,則必須以revert:開頭置鼻,后面跟著被撤銷 Commit 的 Header
revert: type(scope): some comment
This reverts commit bfe307ce57d87677c6c473c228e6c2ed8b81dcec.
Body部分的格式是固定的,必須寫成This reverts commit <hash>.
蜓竹,其中的hash是被撤銷 commit 的 HSHA
標(biāo)識符箕母。
如果當(dāng)前 commit 與被撤銷的 commit,在同一個發(fā)布(release)里面俱济,那么它們都不會出現(xiàn)在 Change log 里面嘶是。如果兩者在不同的發(fā)布,那么當(dāng)前 commit蛛碌,會出現(xiàn)在 Change log 的Reverts小標(biāo)題下面
2. 使用commitizen來執(zhí)行規(guī)范
前提需要安裝node(官網(wǎng)下載地址)
如果看到 EACCES
錯誤, 請閱讀 fixing npm permissions 獲取幫助)
以下操作均參考cz-cli倉庫所給出的指導(dǎo),需要獲取更加詳細(xì)的信息請前往cz-cli.
- 全局安裝
commitizen
node模塊
npm install -g commitizen
如果你的項(xiàng)目不是node
項(xiàng)目,下面的內(nèi)容可以直接忽略,請直接翻到 一級標(biāo)題3
- 在項(xiàng)目目錄下運(yùn)行命令
commitizen init cz-conventional-changelog --save --save-exact
- 此時可能會報找不到
package.json
的錯誤,使用下面命令來自動生成一個項(xiàng)目的package,然后在運(yùn)行2中的命令.
npm init --yes
運(yùn)行完以上一律使用git cz
代替git commit
來提交代碼,同時會顯示一下選項(xiàng)來自動生成符合格式的commit message.
PS ~/git> git cz
cz-cli@2.10.1, cz-conventional-changelog@2.1.0
Line 1 will be cropped at 100 characters. All other lines will be wrapped after 100 characters.
? 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
(Move up and down to reveal more choices)
按照提示,你可以寫出規(guī)范的message了
idea有插件可以使用git commit template(可能需要科~學(xué)
上網(wǎng))
commitizen同時可以檢查commit message是否符合格式.
生成change log,還又一些高級用法比如ghooks
這里就不細(xì)說了.詳細(xì)請查看參考鏈接和validate-commit-msg
- 現(xiàn)在項(xiàng)目中可能多出來
dir:node_nodules, file:package.json, package-lock.json
這些目錄和文件,這是node安裝模塊產(chǎn)生的,如果不是node項(xiàng)目都可以忽略掉,熟悉node的同學(xué)肯定都知道哪些是有用的了.
3. 在非node項(xiàng)目中,優(yōu)雅的使用git-cz
此處可接2.1
上下文,安裝git-cz
npm install -g commitizen git-cz
使用git-cz
這個adapter
還可以自定義一些內(nèi)容,默認(rèn)也會附帶一些表情,如下圖
詳細(xì)的設(shè)置操作請參考倉庫 git-cz 給出的詳細(xì)信息
參考鏈接:
阮一峰的博客
Jartto's blog
cz-cli