Git 寫出好的 commit message

原文:https://ruby-china.org/topics/15737

爲什幺要關注提交信息

  • 加快 Reviewing Code 的過程

  • 幫助我們寫好 release note

  • 5年後幫你快速想起來某個分支,tag 或者 commit 增加了什麼功能,改變了哪些代碼

  • 讓其他的開發者在運行 git blame 的時候想跪謝

  • 總之一個好的提交信息,會幫助你提高項目的整體質量

基本要求

  • 第一行應該少於50個字。 隨後是一個空行 第一行題目也可以寫成:Fix issue #8976

  • 喜歡用 vim 的哥們把下面這行代碼加入 .vimrc 文件中,來檢查拼寫和自動折行

autocmd Filetype gitcommit setlocal spell textwidth=72

  • 永遠不在 git commit 上增加 -m 或 –message= 參數,而單獨寫提交信息

一個不好的例子 git commit -m “Fix login bug”

一個推薦的 commit message 應該是這樣:

Redirect user to the requested page after login

https://trello.com/path/to/relevant/card

Users were being redirected to the home page after login, which is less
useful than redirecting to the page they had originally requested before
being redirected to the login form.

  • Store requested path in a session variable
  • Redirect to the stored location after successfully logging in the user

  • 註釋最好包含一個連接指向你們項目的 issue/story/card。一個完整的連接比一個 issue numbers 更好

  • 提交信息中包含一個簡短的故事,能讓別人更容易理解你的項目

註釋要回答如下信息

爲什麼這次修改是必要的?

要告訴 Reviewers,你的提交包含什麼改變。讓他們更容易審覈代碼和忽略無關的改變。

如何解決的問題?

這可不是說技術細節。看下面的兩個例子:

Introduce a red/black tree to increase search speed

Remove , which was causing

如果你的修改特別明顯,就可以忽略這個。

這些變化可能影響哪些地方?

這是你最需要回答的問題。因爲它會幫你發現在某個 branch 或 commit 中的做了過多的改動。一個提交儘量只做1,2個變化。

你的團隊應該有一個自己的行爲規則,規定每個 commit 和 branch 最多能含有多少個功能修改。

小提示

  • 使用 fix, add, change 而不是 fixed, added, changed

  • 永遠別忘了第2行是空行

  • 用 Line break 來分割提交信息,讓它在某些軟件裏面更容易讀

  • 請將每次提交限定於完成一次邏輯功能。並且可能的話,適當地分解爲多次小更新,以便每次小型提交都更易於理解。

例子

Fix bug where user can’t signup.

[Bug #2873942]

Users were unable to register if they hadn’t visited the plans
and pricing page because we expected that tracking
information to exist for the logs we create after a user
signs up. I fixed this by adding a check to the logger
to ensure that if that information was not available
we weren’t trying to write it.

Redirect user to the requested page after login

https://trello.com/path/to/relevant/card

Users were being redirected to the home page after login, which is less
useful than redirecting to the page they had originally requested before
being redirected to the login form.

  • Store requested path in a session variable
  • Redirect to the stored location after successfully logging in the user

本文參考閱讀

  • A Note About Git Commit Messages
  • Writing good commit messages
  • Proper Git Commit Messages and an Elegant Git History
  • A Better Git Commit
  • 5 Useful Tips For A Better Commit Message
  • whatthecommit

  • 寫好Git Commit信息的7個建議

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章