版本庫(kù)提交信息規(guī)范
以下規(guī)范是社區(qū)使用最廣的 Angular
規(guī)范恕曲。
一般提交命令我們使用如 git commit- m "feat(user): add user login"
其中當(dāng)前 commit message
信息包括三部分:type
(必需)、scope
(可選)识窿、 subject
(必需)
type
type
用于說(shuō)明 commit
的類別吹埠,允許使用下面 8
個(gè)標(biāo)識(shí)第步。
feat: # 新功能(feature)
fix: # 修補(bǔ) bug
docs: # 文檔(documentation)
style: # 格式(不影響代碼執(zhí)行的變動(dòng),非css樣式專用)
refactor: # 重構(gòu)(既不是新增功能缘琅,也不是修改 bug 的代碼變動(dòng))
test: # 增加測(cè)試
chore: # 構(gòu)建過(guò)程或輔助工具的變動(dòng)
revert: # 對(duì)之前修改代碼 commit 記錄的還原
scope
scope
用于說(shuō)明 commit
的影響范圍粘都,比如數(shù)據(jù)層、控制層刷袍、視圖層翩隧、功能模塊等,視項(xiàng)目的不同而不同呻纹。
例如:
- 新增用戶管理的新增用戶功能
$ git commit -m "feat(user): add addUser"
- 修復(fù)了用戶管理的新增用戶功能的
bug
$ git commit -m "fix(user): addUser"
- 更新文檔說(shuō)明
$ git commit -m "docs(readme): update readme"
- 調(diào)整新增用戶代碼格式(代碼換行或者刪除換行)
$ git commit -m "style(user/add): wrap line"
- 增加單元測(cè)試代碼
$ git commit -m "test: add test"
- 配置或者修改自動(dòng)化部署文件或者構(gòu)建工具
$ git commit -m "chore: cicd"
$ git commit -m "chore: add vue-router"
還有一種是相對(duì)來(lái)說(shuō)比較特殊的對(duì)之前 commit
代碼的回退堆生,比如還原之前修改 bug
的錯(cuò)誤提交
$ git commit -m "revert: fix(user): addUser"
subject
subject
是 commit
目的的簡(jiǎn)短描述,不超過(guò) 50
個(gè)字符
$ git commit -m "feat(course): 完成課程管理模塊"
issue_id and pr_id
github
與 gitlab
還提供了自動(dòng)識(shí)別 issue
與 pr
鏈接的功能雷酪。
在提交信息后面加上對(duì)應(yīng)的 issue_id
或 pr_id
顽频,能夠自動(dòng)識(shí)別為對(duì)應(yīng)鏈接,點(diǎn)擊可跳轉(zhuǎn)到對(duì)應(yīng)界面太闺。
$ git commit -m "fix(user): check user pwd (#1)"
這樣的話糯景,會(huì)自動(dòng)識(shí)別 #1
為鏈接,跳到對(duì)應(yīng)的 issue
或者 pr
頁(yè)面省骂。
自動(dòng)驗(yàn)證提交信息規(guī)范
為了達(dá)到提交信息規(guī)范的校驗(yàn)效果蟀淮,我們使用配置本地 git hooks
模板的方法,只需設(shè)置一次钞澳,之后從 github
或者 gitlab
上 clone
下來(lái)的代碼都會(huì)沿用當(dāng)前 hooks
配置怠惶。
下面命令操作環(huán)境為 git bash
:
-
github
克隆倉(cāng)庫(kù)到本地(gitlab
同理)
$ git clone https://github.com/jwchan1996/commit-msg.git
$ cd /commit-msg
- 創(chuàng)建
git
鉤子模板文件夾
$ mkdir -p ~/.git_template/hooks
- 復(fù)制當(dāng)前配置好的
commit-msg
hooks
文件到模板
$ cp commit-msg ~/.git_template/hooks
- 設(shè)置
git
的初始化模板的使用模板
$ git config --global init.templatedir ~/.git_template
具體 commit-msg
文件可以點(diǎn)擊查看詳情