Skip to content

Git Commit 規範

tags: Git 規範

約定式提交(Conventional Commits)

用於 Git 提交訊息的規範和最佳實踐, 由 AngularJS的Contributor Guide 演變而來

目的

  • 提供更清晰的提交歷史
  • 自動化版本管理
  • 改善專案協作
  • 更容易理解代碼變更

Commit Message 規範組成

<type>([optional scope]): <description>

[optional body]

[optional footer(s)]
<type>([optional scope]): <description> [optional body] [optional footer(s)]
Commit 規範組成說明required
typecommit 類別
scopecommit 提交的範圍
descriptioncommit 簡短描述
body若有需要時從第2行開始寫,針對此次commit內容的詳細說明
footer若為fix類別,建議要加上issue編號,在 Azure DevOps 上可自動追蹤, 例: #123

Commit Type

Commit Type說明範例
feat新增功能或需求異動 (feature),例如實作一個新功能或因應需求變更的調整feat: 新增使用者登入功能feat: 調整首頁按鈕符合新需求
fix修補 bug (bug fix),修正程式中的錯誤或問題,使其正常運作fix: 修復結帳頁面金額計算錯誤
style格式調整 (style),不影響程式運行的變更,如調整 CSS、空格或補上分號等排版工作style: 修正 CSS 縮排格式style: 補上遺漏的分號
refactor程式碼重構 (refactor),改進現有程式碼結構,既非新增功能也非修補 bug,而是提升程式品質refactor: 重構資料庫查詢邏輯以提升可讀性
docs文件更新 (documentation),例如新增註解、編寫說明文件或更新 md 檔docs: 更新 API 使用說明文件docs: 在程式碼中補充註解
perf效能改善 (performance),優化程式碼以提升運行速度或效率perf: 優化圖片載入速度
chore雜務調整 (chore),例如更新套件版本、修改建構流程、調整 CICD 的 yml 檔或 Dockerfile,這類不直接影響系統的維護工作chore: 更新 npm 套件版本到最新chore: 修改 Dockerfile 設定
revert撤銷回覆 (revert),取消先前的 commit,例如「revert: feat(某功能):某某描述 (回覆版本:xxxx)」revert: feat(login): 新增登入功能 (回覆版本:abc123)
test測試新增 (test),例如補充單元測試或新增測試程式以驗證功能test: 新增結帳功能的單元測試

重大變更

基於 <type>[optional scope] 後加 ! 或是於 [optional body] 填寫 BREAKING CHANGE (要全大寫)

Example

基本用法

feat:  add user authentication

帶 scope 的用法

feat(login): implement two-factor authentication

詳細描述的用法

fix: correct minor typos in code

more precise changes that were missed in the previous commit.

Refs #133

包含 breaking change 的用法

feat(api)!: migrate to new authentication system
feat(api)!: migrate to new authentication system

BREAKING CHANGE: The previous authentication method 
is no longer supported. Users must update their 
integration to use the new JWT-based approach.

工具

參考資料