【Git】使用 Conventional Commits 規範優化 commit message

【Git】使用 Conventional Commits 規範優化 commit message

本篇重點

  • Conventional Commits 是建立在 Git Commit Message 上的輕量規範,讓 Commit 同時具有人類與機器可讀的結構
  • 基本格式為 <type>[optional scope]: <description>bodyfooter 皆為選填
  • 彙整主要 commit type 類型及其適用情境
  • Conventional Commits 能讓 Commit History 更容易閱讀,還能提供 Changelog、Semantic Versioning、Release 與 CI/CD 自動化所需的結構化資訊

Conventional Commits 核心結構

Conventional Commits 基本訊息結構

核心欄位說明:

欄位必要性說明
type必要描述 Commit 的變更類型
scope選填描述變更影響的範圍或區域
description必要簡短描述變更內容
body選填詳細說明變更原因以及與先前行為的對比
footer選填說明變更額外資訊

Type 類型定義

Conventional Commits 並沒有強制所有專案必須使用一組完整固定的 Type,featfix 是規範中具有明確定義的兩個核心 Type。docsrefactortestchore 等則是目前非常常見的實務分類。

Type定義與用途範例
feat新增功能(Feature)feat: add user login
fix修復 Bugfix: handle invalid token
docs僅變更文件內容docs: update API guide
refactor重構程式碼(既不新增功能也不修復 Bug)refactor: simplify auth flow
test新增或修整測試案例test: add login tests
chore維護性工作chore: update dependencies
style不影響程式碼邏輯的格式調整(空格、縮排等)style: format source files
perf提升效能的程式碼變更perf: reduce query overhead
build影響建置系統或外部依賴項的變更build: update webpack
ci變更 CI 設定檔或腳本ci: update GitHub Actions

健忘筆記

style 在 Conventional Commits 中通常代表不影響程式語意的格式調整,例如空白、排版或格式化,而不是 UI Style 修改,UI Style 新增/修改應該使用 feat

Scope 修改範圍

描述變更影響的範圍或區域

Conventional Commits
1
2
3
feat(auth): add OAuth login
fix(api): handle timeout
refactor(database): simplify connection pool

適合在大型專案或模組較多時使用;小型專案也可以省略。

Conventional Commits
1
feat: add OAuth login

Description 描述變更

負責描述「這次 Commit 改了什麼」,保持短而明確的內容

Conventional Commits
1
fix: handle expired access token

避免使用無法判斷變更內容的描述

Conventional Commits
1
fix: update code

Body 補充原因與背景

Description 不足以說明修改原因時,增加 Body 補充說明

Conventional Commits
1
2
3
fix(auth): reject expired access tokens

The API previously accepted expired tokens because expiration validation was only performed during refresh.

說明 commit 額外資訊,標註破壞性變更(Breaking Changes)、記錄 Issue/PR 關聯或其他 Metadata:

Conventional Commits
1
2
3
fix(api): handle invalid request payload

Refs: #123

也可以記錄 Breaking Change:

Conventional Commits
1
2
3
feat(api): change authentication response

BREAKING CHANGE: authentication now returns an access token instead of a session identifier.

破壞性變更標示方式

當變更包含打破向下相容性(Breaking Changes)的重大修改時,必須在 Commit 訊息中明確標示,以利版本自動升級判定(MAJOR),兩種方法可同時使用也可擇一使用。

驚嘆號標示(簡短表示)

type(或 scope)之後、冒號之前的位置加上 !

Conventional Commits
1
feat(api)!: remove legacy user authentication endpoint

在 Body 後方空一行,於 Footer 開頭使用大寫 BREAKING CHANGE:,後方緊接變更的詳細說明或遷移指南。

Conventional Commits
1
2
3
feat(api): update user authentication signature

BREAKING CHANGE: The `authToken` string format has changed to JWT. Existing tokens will be invalidated.

結論

使用 Conventional Commit 的好處

Conventional Commits 透過統一的結構限制,降低了團隊成員理解變更紀錄的認知成本。更重要的是,格式化的 Commit Message 成為現代自動化工具鏈的關鍵基礎,讓專案的版本號變更、Changelog 生成與 Release 流程能夠無縫整合到 CI/CD Pipeline,提升軟體交付的精確度與效率!

延伸閱讀

【Git】使用 Conventional Commits 規範優化 commit message

https://forgetfulengineer.github.io/Other/Git/conventional-commits-commit-message-guide/

發表於

2026-08-19

更新於

2026-08-19

許可協議


你可能也想看

【GitHub】解析 Reference System:Issue、Pull Request、Commit 如何建立關聯
【Git、Hexo】deploy github 檔名大小寫問題
【Git】了解 git config 設定

評論

複製完成