Git Commit 規範

LvLaoTou發表於2024-03-27

背景

git規定提交時必須要寫提交資訊,作為改動說明儲存在 commit 歷史中,方便回溯。規範的 log 不僅有助於他人 review,還可以有效的輸出 change_log甚至對於專案的研發質量都有很大的提升。參考目前比較流行的Angular團隊的commit規範

Angular commit規範格式

<type>(<scope>): <subject>
// 空一行
<body>
// 空一行
<footer>

分別對應 Commit message 的三個部分:Header,Body 和 Footer

Header

Header 部分只有一行

包括三個欄位:type(必需)、scope(可選)和subject(必需)

  • type::用於說明 commit 的型別,一般有以下幾種

    • feat: 新增feature
    • fix: 修復bug
    • docs: 僅僅修改了文件,如readme.md
    • style: 僅僅是對格式進行修改,如逗號、縮排、空格等。不改變程式碼邏輯
    • refactor: 程式碼重構,沒有新增功能或修復bug
    • perf: 最佳化相關,如提升效能、使用者體驗等。
    • test: 測試用例,包括單元測試、整合測試。
    • chore: 改變構建流程、或者增加依賴庫、工具等。
    • revert: 版本回滾
  • scope:用於說明 commit 影響的範圍

    比如:檢視層、控制層、資料層、docs、config、plugin、util、test

  • subject:commit 目的的簡短描述

    一般不超過50個字

Body

補充 subject 新增詳細說明,可以分成多行

適當增加原因、目的等相關因素,也可不寫

參考

 #  body: 72-character wrapped. This should answer:
# * Why was this change necessary?
# * How does it address the problem?
# * Are there any side effects?
# initial commit

Footer

當有當前程式碼與上一個版本不相容(Breaking Change)時必須在這裡描述清楚

修復的 bug(關閉issue)或是連結到相關文件,如 Closes #1, Closes #2, #3

使用

  1. 設定git 提交資訊模板

新建 .gitmessage.txt(模板檔案) ,參考內容如下

# headr: <type>(<scope>): <subject>
# - type: feat, fix, docs, style, refactor, test, chore
# - scope: can be empty
# - 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 issue.
# - BREAKING CHANGE
#
  1. 配置提交資訊模板
//這個命令只能設定當前分支的提交模板
git config commit.template [模板檔名]
//這個命令能設定全域性的提交模板,注意global前面是兩槓
git config --global commit.template [模板檔名]

Idea 外掛

  1. 安裝外掛
Untitled
  1. 使用
Untitled
Untitled
Untitled
Untitled

本文使用 markdown.com.cn 排版

相關文章