優化 Git Commit Message

git-logo

目前不少項目都是經過 Git 進行管理的,Git 每次提交代碼的過程當中 提交說明 commit message 是必須的。但僅僅必須是不夠的,好的提交說明能夠幫助咱們提升項目的總體質量。git

做用與優勢

提交說明最首要的目的是幫助 提交者 說明本次提交的目的,而規範的說明信息有幾個好處。github

  1. 提供完整的信息,幫忙快速定位問題
  2. 過濾某些 commit ,快速查找有用信息
  3. 直接從 commit 信息生成 Change log
  4. 加快 Code Review 的過程

基本要求

  • 第一行應該少於 50 個字。隨後是一個空行
  • 永遠不在 git commit 上增長 -m <msg> 或者 --message=<msg> 參數,而須要單獨寫提交信息

好的提交說明要包含下面的內容:shell

  1. 爲何要提交此次修改?
  2. 怎麼解決的問題?
  3. 可能影響哪些內容?

Angular 規範 CommitMsg 格式

Angular 主要有三個格式,其中 Head 是必須的,bodyfooter 是可選的。segmentfault

# 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 個標識。app

  • feat 新功能
  • fix 修補 bug
  • docs 文檔
  • style 格式
  • refactor 重構
  • test 增長測試
  • chore 構建過程、輔助工具
  • perf 提升性能

若是 typefeatfix ,則該 commit 信息將確定出如今 change log 之中。ide

scope 用於說明 commit 影響的範圍,好比影響哪一層、哪一個包中的內容或者使用哪些方案的實例。工具

subject 是關於 commit 信息的簡短描述,不超過 50 個字。性能

Body

Body 部分是針對本次 commit 的詳細描述,能夠多行,要表達清楚變更的動機、與以前行爲的對比。測試

Footer

用於不兼容變更和關閉 Issue 。this

使用 gitemoji 進行美化

雖然純文字的提交說明已經很是明確了,可是卻比較單調,並且在一些內容上感受少了一些步驟,因而在現有的內容上進行少許的修改以更美觀的清晰。

type 主要將原來的 chore 進行了拆分,增長了 cireview 兩個內容,用來補充項目開發過程當中的流程。其它的還有一些內容,能夠參照 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:

相關工具

其它

使用 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"

使用命名查詢的效果以下:

lg.png

文章首發於:https://www.zucchiniy.cn

相關文章
相關標籤/搜索