开发者社区> crazyacking> 正文

写出好的 commit message

简介: 为何要关注提交信息 加快Reviewing Code的过程 帮助我们写好release note 5年后帮你快速想起来某个分支,tag或者 commit增加了什么功能,改变了哪些代码 让其他的开发者在运行 git blame 的时候想跪谢 总之一个好的提交信息,会帮助你提高项目的整体质量 基本要求 第一行应该少于50个字。
+关注继续查看

 

为何要关注提交信息

  • 加快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 <msg>--message=<msg> 参数,而单独写提交信息

一个不好的例子 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 <troublesome gem X>, which was causing <specific description of issue introduced by gem>

如果你的修改特别明显,就可以忽略这个。

这些变化可能影响哪些地方?

这是你最需要回答的问题。因为它会帮你发现在某个 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

本文参考阅读

 

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
【异常】svn: E200009: Commit failed (details follow)/both sides of the move must be committed together的解决办法
svn: E200009: Commit failed (details follow)/both sides of the move must be committed together的解决办法
84 0
git 出现错误:kex_exchange_identification: read: Connection reset by peer fatal
git 出现错误:kex_exchange_identification: read: Connection reset by peer fatal
203 0
Git Commit Message规范
Git Commit Message规范
89 0
解决 error: failed to push some refs to 'https://github.com/
Resolve error: failed to push some refs to \'https://github.com/
97 0
Commit request failed Commit failed. Ref must be HEAD and is HEAD
Commit request failed Commit failed. Ref must be HEAD and is HEAD
35 0
使用SAP WebIDE往Github上推送代码修改时遇到错误消息 Commit request failed Commit failed. Ref must be HEAD and is HEAD
使用SAP WebIDE往Github上推送代码修改时遇到错误消息 Commit request failed Commit failed. Ref must be HEAD and is HEAD
67 0
git schnnel failed to receive handshake, SSLTLS connection failed
git schnnel failed to receive handshake, SSLTLS connection failed
148 0
SET UPDATE TASK LOCAL and commit session issue
SET UPDATE TASK LOCAL and commit session issue
58 0
如何处理error message Invalid parametertype used at function XXXX
Created by Jerry Wang, last modified on Jan 08, 2015
78 0
Git生态探索之Commit message 和 Change log 编写最佳实践
背景最近在学习规范如何使用git来更高效的开发,发现一个比较好用的来规范comment的工具,所以想来记录一下。一般来说,commit message 应该清晰明了,说明本次提交的目的。所以需要一些规范来使这些comment变得可读,commitizen则是最近发现的一款比较易用的工具。
1322 0
+关注
crazyacking
PaaS技术、Kubernetes、云计算,架构设计、数据库、数据结构、Unix/Linux编程
文章
问答
视频
文章排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载