git commit的規范


https://www.yuque.com/fe9/basic/nruxq8#6c228def 語雀平台

 

制定一個 git commit 信息的提交規范是開發團隊工作流必不可少的環節。試想一下,如果查看主分支上的歷史庫也就是你查看 git log 的時候,打印出來的信息雜亂無章的話,如果代碼遇到問題,可能需要很大的精力與成本來查找到導致問題的代碼提交,所以團隊需要制定規范來引導成員編寫規范的 commit 信息。

接下來的 commit 信息規范參考了 angularjs 團隊的開發者指引與筆者的工作團隊進行總結,讀者如有需要可以以此為基礎增加或修改成為自己團隊的 commit 規范的一部分。

提交信息基本模板

如果 commit 信息都按照一定的模式進行提交,那么我們就會很容易找到自己想要的信息,模板參考如下:

 

<type>(<scope>): <subject> [<ISSUE_ID>]

<body>

<footer>

 

commit 信息包括三個字段: type (必需), scope(可選) 和 subject(必需)。

  1. type。type 是用於說明該 commit 的類型的,一般我們會規定 type 的類型如下:
  • feat: 新功能(feature)
  • fix: 修復 bug
  • docs: 文檔(documents)
  • style: 代碼格式(不影響代碼運行的格式變動,注意不是指 CSS 的修改)
  • refactor: 重構(既不是新增功能,也不是修改 bug 的代碼變動)
  • test: 提交測試代碼(單元測試,集成測試等)
  • chore: 構建或輔助工具的變動
  • misc: 一些未歸類或不知道將它歸類到什么方面的提交
  1. scope。scope 說明 commit 影響的范圍,比如數據層,控制層,視圖層等等,這個需要視具體場景與項目的不同而靈活變動
  2. subject。subject 是對於該 commit 目的的簡短描述
  • 使用第一人稱現在時的動詞開頭,比如 modify 而不是 modified 或 modifies
  • 首字母小寫,並且結尾不加句號
  1. ISSUEE_ID。這個與公司的需求管理與項目管理有關,假設你的項目放在 github 上,你的需求或者 bug 修復可能會有對應的 issues 記錄,你可以加到你的 commit 信息中如 issue-37938634

 

body 其實就是 subject 的詳細說明,而 footer 中你可以填寫相關的需求管理 issues id。

在企業中一般會對團隊中要做的事情與需求開發使用一個軟件進行管理,好處是可以讓代碼與對應的用戶故事(story)或者需求,bug 進行關聯,便於管理,類似的方案有 github,gitlab,tracker,JIRA 等等,比如在網易某些團隊中就會使用 JIRA 加上 gitlab 來進行團隊管理。

commit message 的規范性是很重要的,對於自己養成良好的編程習慣很有幫助,但是沒有必要強制完全遵循開源團隊的規范,畢竟每個團隊與個人的情況不同,博采眾長即可,當然你也可以使用像 commitlint 這樣的校驗工具從工具層面上來強制執行某些規范,這里就不展開講了,有興趣的讀者可以查閱相關資料並使用到自己團隊的實踐中。

 

 

https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration

commit.template

If you set this to the path of a file on your system, Git will use that file as the default initial message when you commit. The value in creating a custom commit template is that you can use it to remind yourself (or others) of the proper format and style when creating a commit message.

For instance, consider a template file at ~/.gitmessage.txt that looks like this:

Subject line (try to keep under 50 characters)

Multi-line description of commit,
feel free to be detailed.

[Ticket: X]

Note how this commit template reminds the committer to keep the subject line short (for the sake of git log --oneline output), to add further detail under that, and to refer to an issue or bug tracker ticket number if one exists.

To tell Git to use it as the default message that appears in your editor when you run git commit, set the commit.template configuration value:

$ git config --global commit.template ~/.gitmessage.txt
$ git commit

Then, your editor will open to something like this for your placeholder commit message when you commit:

Subject line (try to keep under 50 characters)

Multi-line description of commit,
feel free to be detailed.

[Ticket: X]
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
# modified:   lib/test.rb
#
~
~
".git/COMMIT_EDITMSG" 14L, 297C

If your team has a commit-message policy, then putting a template for that policy on your system and configuring Git to use it by default can help increase the chance of that policy being followed regularly.

 

 

Git Commit Msg

Create A Custom Git Commit Template

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM