原文: http://chris.beams.io/posts/git-commit/

介绍:为什么好的commit message很重要

你浏览项目commit message的时候或多或少会有些困扰的地方. 例如, 下面是我早前的commit message:

$ git log --oneline -5 --author cbeams --before "Fri Mar 26 2009"

e5f4b49 Re-adding ConfigurationPostProcessorTests after its brief removal in r814. @Ignore-ing the testCglibClassesAreLoadedJustInTimeForEnhancement() method as it turns out this was one of the culprits in the recent build breakage. The classloader hacking causes subtle downstream effects, breaking unrelated tests. The test method is still useful, but should only be run on a manual basis to ensure CGLIB is not prematurely classloaded, and should not be run as part of the automated build.
2db0f12 fixed two build-breaking issues: + reverted ClassMetadataReadingVisitor to revision 794 + eliminated ConfigurationPostProcessorTests until further investigation determines why it causes downstream tests to fail (such as the seemingly unrelated ClassPathXmlApplicationContextTests)
147709f Tweaks to package-info.java files
22b25e0 Consolidated Util and MutableAnnotationUtils classes into existing AsmUtils
7f96f57 polishing

再来看看我最近的commit message:

$ git log --oneline -5 --author pwebb --before "Sat Aug 30 2014"

5ba3db6 Fix failing CompositePropertySourceTests
84564a0 Rework @PropertySource early parsing logic
e142fd1 Add tests for ImportSelector meta-data
887815f Update docbook dependency and generate epub
ac8326d Polish mockito usage

显然第二种更易读一些.第一种长度和形式都不一样, 第二种风格比较统一.

一个好的commit message应该遵循的7条规则

  1. 主题和内容用空白行隔开
  2. 主题的限制在50个字符内
  3. 主题首字母大写
  4. 主题结尾不要有标点符号
  5. 主题用祈始语句
  6. 正文一行最多72个字符
  7. 内容里解释做了什么,和为什么这么做

例子:

Summarize changes in around 50 characters or less

More detailed explanatory text, if necessary. Wrap it to about 72
characters or so. In some contexts, the first line is treated as the
subject of the commit and the rest of the text as the body. The
blank line separating the summary from the body is critical (unless
you omit the body entirely); various tools like `log`, `shortlog`
and `rebase` can get confused if you run the two together. Explain the problem that this commit is solving. Focus on why you
are making this change as opposed to how (the code explains that).
Are there side effects or other unintuitive consequenses of this
change? Here's the place to explain them. Further paragraphs come after blank lines. - Bullet points are okay, too - Typically a hyphen or asterisk is used for the bullet, preceded
by a single space, with blank lines in between, but conventions
vary here If you use an issue tracker, put references to them at the bottom,
like this: Resolves: #123
See also: #456, #789

1.主题和内容用空白行隔开

来子git commit manapage:

不是必须, 但是最好是用一个简短的行(小于50个字符)作为主题描述这次修改, 然后空一行后写详细的描述. 第一个空白行上面的文字会被git做为标题对待.

首先, 不是每一个commit message都需要主题和内容. 有的时候一行就行了, 特别是在修改比较简单的情况下.例如:

Fix typo in introduction to user guide

不需要多说什么. 如果实在想看这个拼写是什么, 可以使用git show git diff git log -p.

如果你的committing是上面这种很简单的一个主题就能描述清楚的事情, 使用git commit -m就行了:

$ git commit -m"Fix typo in introduction to user guide"

如果你的committing比较复杂, 需要多点内容进行描述的时候,使用-m就不方便了. 这是你commit的时候不要加-m在弹出来的编辑器里面进行描述会比较好.

主题和内容用空白行隔开是好的. 下面是一个完整的log纪录:

$ git log
commit 42e769bdf4894310333942ffc5a15151222a87be
Author: Kevin Flynn <kevin@flynnsarcade.com>
Date: Fri Jan 01 00:00:00 1982 -0200 Derezz the master control program MCP turned out to be evil and had become intent on world domination.
This commit throws Tron's disc into MCP (causing its deresolution)
and turns it back into a chess game.

使用 git log --oneline, 就只打印出来主题行了:

$ git log --oneline
42e769 Derezz the master control program

使用git shortlog 按作者分组显示的时候也只会打印出主题行:

$ git shortlog
Kevin Flynn (1):
Derezz the master control program Alan Bradley (1):
Introduce security program "Tron" Ed Dillinger (3):
Rename chess program to "MCP"
Modify chess program
Upgrade chess program Walter Gibbs (1):
Introduce protoype chess program

2.限制主题在50个字符之内

50个字符不是一个硬性规定. 把主题限制在50个字符之内会让这个主题更易读, 而且还会使得作者尽量用精简的语句来描述发什么了什么.

tip:如果你很难简短的慨括这个修改, 你可能是一次commit了太多的修改了. 要尽量做到一次commit就干一件事情做到原子提交.

GitHub的界面就知道这个约定. 如果你的message超过了50个字符, 他会给你一个警告:

标题超过69这个长度之后的字符会被...:

3.主题首字母大写

主题的首字母大写. 例如:

Accelerate to 88 miles per hour

替代

accelerate to 88 miles per hour

4.主题的结尾不要有标点符号  

结尾的标点一点用处都没有, 还会从50个字符里面占掉1个字符.例如:

Open the pod bay doors

替代

open the pod bay doors.

5.主题用祈始语句

祈始语句就像是命令的语句.

  • Clean your room
  • Close the door
  • Take out the trash

祈始语句好像有点粗鲁, 但是它直接明了. 另外merge commit的message也是祈始语句:

Merge branch 'myfeature'

git revert也是祈始语句:

Revert "Add the thing with the stuff"

This reverts commit cc87791524aedd593cff5a74532befe7ab69ce9d.

  

[译]How to Write a Git Commit Message的更多相关文章

  1. 如何写好git commit message

    1.触发事件 我有这样一个版本库,里面包含两个学习用的练习项目:BookStore(以下简称BS)和PictureFriend(以下简称PF) 我在更改PF以后,未进行提交,同时又到BS中优化了一下文 ...

  2. git第四节----git commit message

    @git  commit message 什么是git commit message :git commit -m '每次提交时编辑的内容' git commit message的好处:      1 ...

  3. Git Commit Message 规范

    今天来说说团队开发中,对于 Git commit message 规范问题. 社区上有各种 Commit message 的规范,本文介绍 Angular 规范,目前使用较广,比较合理和系统化,并且有 ...

  4. 我是怎么写 Git Commit message 的?

    目录 作用 用的什么规范? type scope subject body footer 参考文章 用的什么辅助工具? 作用 编写格式化的 commit message 能够大大提高代码的维护效率. ...

  5. Git commit message和工作流规范

    目的 统一团队Git commit日志标准,便于后续代码review,版本发布以及日志自动化生成等等. 统一团队的Git工作流,包括分支使用.tag规范.issue等 Git commit日志参考案例 ...

  6. git使用总结(包含git commit message 和 changelog 工具的介绍)

    [git的配置] 1.配置用户名和邮箱: 分为全局配置和局部配置 --system 系统配置  --global 全局配置    --local 局部配置 Git读取时:优先从local>glo ...

  7. commitizen和cz-customizable配置git commit message

    起因 团队对提交的commit message格式有约定俗称的要求,但是没有一个统一的规范,导致大家提交的commit message或多或少不太一样.因此,需要一个工具来帮助大家统一commit m ...

  8. 优化 Git Commit Message

    目前很多项目都是通过 Git 进行管理的,Git 每次提交代码的过程中 提交说明 commit message 是必须的.但仅仅必须是不够的,好的提交说明可以帮助我们提高项目的整体质量. 作用与优点 ...

  9. IDEA 中 Git Commit message 编写

    IDEA安装插件 Git Commit Template 1. HeaderHeader的部分只有一行,包括三个字段: type(必需), scope(可选), subject(必需) 对应到idea ...

随机推荐

  1. redis持久化RDB和AOF

    Redis 持久化: 提供了多种不同级别的持久化方式:一种是RDB,另一种是AOF. RDB 持久化可以在指定的时间间隔内生成数据集的时间点快照(point-in-time snapshot). AO ...

  2. linux基本知识2

    date:时间管理 linux时钟: 硬件时钟:hwclock -s:硬件时钟到系统时钟   -w:系统时钟到硬件时钟 系统时钟:date 如何查看是外部命令还是内部命令: type COMMAND ...

  3. C++实例讲解Binder通信

    binder是android里面的通信机制,这就不说它如何如何好了,Goog已经说过了,这里不多说.binder是一个面向对象的编程方法,大量使用虚函数类.最近研究binder看到一网友写的,就借鉴一 ...

  4. SQL Server 修改表结构后无法保存的老问题

    在修改表结构后无法保存,这是每次重装SQL Server后都会遇到的问题,好记性不如烂笔头,在这里记一下吧. 保存修改了的表结构时会提示“不允许保存更改.您所做的更改要求删除并重新创建以下表.您对无法 ...

  5. css实现页面元素居中

    水平居中 对于已知宽度的块级元素实现水平居中只需要设置 margin-left:auto; margin-right:auto; 对于多个块级元素实现水平居中只需要设置 //1 父类{ text-al ...

  6. Windows操作系统下远程连接MySQL数据库

    用Eclipse做一个后台项目,但是数据库不想放在本地电脑,于是买了一个腾讯云服务器(学生有优惠,挺便宜的),装上MySQL数据库,但是测试连接的时候,发现总是连接不是上,但是本地数据库可以连接,于是 ...

  7. ConcurrentHashMap内存泄漏问题

    问题背景 上周,同事写了一段ConcurrentHashMap的测试代码,说往map里放了32个元素就内存溢出了,我大致看了一下他的代码及运行的jvm参数,觉得很奇怪,于是就自己捣鼓了一下.首先上一段 ...

  8. Maven命令

    1. mvn help:describe 你是否因为记不清某个插件有哪些goal而痛苦过,你是否因为想不起某个goal有哪些参数而苦恼,那就试试这个命令吧,它会告诉你一切的. 参数: 1. -Dplu ...

  9. mybatis 调用存储过程

    <select id="selectGenCodeBySql" parameterType="hashmap" statementType="C ...

  10. Java多线程与并发库高级应用-面试题

    第一题:现有的程序代码模拟产生了16个日志对象,并且需要运行16秒才能打印完这些日志,请在程序中增加4个线程去调用parseLog()方法来分头打印这16个日志对象,程序只需要运行4秒即可打印完这些日 ...