目前很多項目都是通過 Git 進行管理的,Git 每次提交代碼的過程中 提交說明 commit message 是必須的。但僅僅必須是不夠的,好的提交說明可以幫助我們提高項目的整體質量。
作用與優點
提交說明最首要的目的是幫助 提交者 說明本次提交的目的,而規范的說明信息有幾個好處。
- 提供完整的信息,幫忙快速定位問題
- 過濾某些 commit ,快速查找有用信息
- 直接從 commit 信息生成 Change log
- 加快 Code Review 的過程
基本要求
- 第一行應該少於 50 個字。隨后是一個空行
- 永遠不在
git commit
上增加-m <msg>
或者--message=<msg>
參數,而需要單獨寫提交信息
好的提交說明要包含下面的內容:
- 為什么要提交這次修改?
- 怎么解決的問題?
- 可能影響哪些內容?
Angular 規范 CommitMsg 格式
Angular 主要有三個格式,其中 Head 是必須的,body 和 footer 是可選的。
# head: <type>(<scope>): <subject>
# - type: feat, fix, docs, style, refactor, test, chore
# - scope: can be empty (eg. if the change is a global or difficult to assign to a single component)
# - subject: start with verb (such as 'change'), 50-character line
#
# body: 72-character wrapped. This should answer:
# * Why was this change necessary?
# * How does it address the problem?
# * Are there any side effects?
#
# footer:
# - Include a link to the ticket, if any.
# - BREAKING CHANGE
#
Header
其中 type 用來說明 commit 的類別,只允許使用下面的 7 個標識。
- feat 新功能
- fix 修補 bug
- docs 文檔
- style 格式
- refactor 重構
- test 增加測試
- chore 構建過程、輔助工具
- perf 提高性能
如果 type 為 feat 和 fix ,則該 commit 信息將肯定出現在 change log 之中。
scope 用於說明 commit 影響的范圍,比如影響哪一層、哪個包中的內容或者使用哪些方案的實例。
subject 是關於 commit 信息的簡短描述,不超過 50 個字。
Body
Body 部分是針對本次 commit 的詳細描述,可以多行,要表達清楚變動的動機、與之前行為的對比。
Footer
用於不兼容變動和關閉 Issue 。
使用 gitemoji 進行美化
雖然純文字的提交說明已經非常明確了,但是卻比較單調,而且在一些內容上感覺少了一些步驟,於是在現有的內容上進行少量的修改以更美觀的清晰。
type 主要將原來的 chore 進行了拆分,增加了 ci 和 review 兩個內容,用來補充項目開發過程中的流程。其它的還有一些內容,可以參照 gitemoji 。
- feat 新功能
:sparkles:
✨ - fix 修補 bug
:bug:
🐛 - docs 文檔
:memo:
📝 - style 格式
:art:
🎨 - refactor 重構
:recycle:
♻ - test 增加測試
:white_check_mark:
✅ - ci 持續集成
:construction_Worker:
👷 - review 完成 review
:ok_hand:
👌 - perf 提高性能
:zap:
⚡
相關工具
- commitizen 撰寫合格的 Commit Message 的工具
- commitlint 檢查 Commit Message 是否符合格式的工具
- conventional-changelog 生成 Change log 的工具
- gitmoji 美化 Commit Message 的工具
其它
使用 git log 查詢的時候,看到的內容比較簡單,可以通過下面的命令將信息格式化輸出。
alias lg="git log --graph --abbrev-commit --decorate --date=relative --format=format:'%C(red)%h%C(r) —— %C(bold blue)%an%C(r): %C(white)%s%C(r) %C(dim white) %C(bold green)(%ar)%C(r) %C(bold yellow)%d%C(r)' --all"
使用命名查詢的效果如下: