版本庫提交信息規范
以下規范是社區使用最廣的 Angular
規范。
一般提交命令我們使用如 git commit- m "feat(user): add user login"
其中當前 commit message
信息包括三部分:type
(必需)、scope
(可選)、 subject
(必需)
type
type
用于說明 commit
的類別,允許使用下面 8
個標識。
feat: # 新功能(feature)
fix: # 修補 bug
docs: # 文檔(documentation)
style: # 格式(不影響代碼執行的變動,非css樣式專用)
refactor: # 重構(既不是新增功能,也不是修改 bug 的代碼變動)
test: # 增加測試
chore: # 構建過程或輔助工具的變動
revert: # 對之前修改代碼 commit 記錄的還原
scope
scope
用于說明 commit
的影響范圍,比如數據層、控制層、視圖層、功能模塊等,視項目的不同而不同。
例如:
- 新增用戶管理的新增用戶功能
$ git commit -m "feat(user): add addUser"
- 修復了用戶管理的新增用戶功能的
bug
$ git commit -m "fix(user): addUser"
- 更新文檔說明
$ git commit -m "docs(readme): update readme"
- 調整新增用戶代碼格式(代碼換行或者刪除換行)
$ git commit -m "style(user/add): wrap line"
- 增加單元測試代碼
$ git commit -m "test: add test"
- 配置或者修改自動化部署文件或者構建工具
$ git commit -m "chore: cicd"
$ git commit -m "chore: add vue-router"
還有一種是相對來說比較特殊的對之前 commit
代碼的回退,比如還原之前修改 bug
的錯誤提交
$ git commit -m "revert: fix(user): addUser"
subject
subject
是 commit
目的的簡短描述,不超過 50
個字符
$ git commit -m "feat(course): 完成課程管理模塊"
issue_id and pr_id
github
與 gitlab
還提供了自動識別 issue
與 pr
鏈接的功能。
在提交信息后面加上對應的 issue_id
或 pr_id
,能夠自動識別為對應鏈接,點擊可跳轉到對應界面。
$ git commit -m "fix(user): check user pwd (#1)"
這樣的話,會自動識別 #1
為鏈接,跳到對應的 issue
或者 pr
頁面。
自動驗證提交信息規范
為了達到提交信息規范的校驗效果,我們使用配置本地 git hooks
模板的方法,只需設置一次,之后從 github
或者 gitlab
上 clone
下來的代碼都會沿用當前 hooks
配置。
下面命令操作環境為 git bash
:
-
github
克隆倉庫到本地(gitlab
同理)
$ git clone https://github.com/jwchan1996/commit-msg.git
$ cd /commit-msg
- 創建
git
鉤子模板文件夾
$ mkdir -p ~/.git_template/hooks
- 復制當前配置好的
commit-msg
hooks
文件到模板
$ cp commit-msg ~/.git_template/hooks
- 設置
git
的初始化模板的使用模板
$ git config --global init.templatedir ~/.git_template
具體 commit-msg
文件可以點擊查看詳情