Git 提交的正确姿势:Commit message 编写指南

Git 提交的正确姿势:Commit message 编写指南

jonathan
2018-12-29 / 0 评论

Git提交的正确姿势:Commit message编写指南

  1. 第一行不超过50个字符
  2. 第二行空一行
  3. 第三行开始是描述信息,每行长度不超过72个字符,超过了自己换行。
  4. 描述信息主要说明:
    • 这个改动为什么是必要的?
    • 这个改动解决了什么问题?
    • 会影响到哪些其他的代码?
  5. 最后最好有一个相应ticket的链接

摘要:Git每次提交代码,都要写Commit message(提交说明),否则就不允许提交。 Git每次提交代码,都要写Commit message(提交说明),否则就不允许提交。

$ git commit -m "hello world"

上面代码的-m参数,就是用来指定commit mesage的。 如果一行不够,可以只执行git commit,就会跳出文本编译器,让你写多行。

$ git commit

基本上,你写什么都行(这里,这里和这里)。

Nov 12, 2013
Tenth Commit
N
nishugames authored 3 months ago
Oct 27, 2013
Ninth Commit
N
nishugames authored 4 months ago
Oct 26, 2013
Eighth Commit
N
nishugames authored 4 months ago
Seventh Commit
nishugames authored 4 months ago
Sixth Commit
N
nishugames authored 4 months ago
Fifth Commit
N
nishugames authored 4 months ago
Oct 24, 2013
Fourth Commit
...
N
nishugames authored 4 months ago
Third commit
N
nishugames authored 4 months ago

但是,一般来说,commit message应该清晰明了,说明本次提交的目的。

Commits on Jan 5, 2016
docs(error/SrootScope/inprog): add missing "Stimeout"
wytesk133 committed with petebacondarwin 10 days ago
docs(tutorial/2): add e2e test missing filename
monkpit committed with petebacondarwin 3 hours ago
revert: feat(Scompile): Allow ES6 classes as controllers with bindTo...
petebacondarwin committed 3 hours ago
fix(SanimateCss): only (de)register listeners when events have been a..
Narretz committed 25 days ago
fix(loader): use 'false' as default value for transclude' in compone...
sarod committed with petebacondarwin 18 days ago
Commits on Jan 3, 2016
feat(Scompile): Allow ES6 classes as controllers with bind ToControl..
Igalfaso committed 21 days ago
chore(*): Updated year in licence
leuanG committed with Igalfaso 3 days ago
Commits on Jan 1, 2016
docs: reorganize information about interpolation
Narretz committed on Dec 6, 2015
docs: add docs for ngPattern, ngMinlength, ngMaxlength
Narretz committed on Sep 24, 2015
docs(SinterpolateProvider): remove superfluous ng-app attribute
Narretz committed 4 days ago

目前,社区有多种Commit message的写法规范。本文介绍Angular规范(见上图),这是目前使用最广的写法,比较合理和系统化,并且有配套的工具。

一、Commit message的作用

格式化的Commit message,有几个好处。

(1)提供更多的历史信息,方便快速浏览

比如,下面的命令显示上次发布后的变动,每个commit占据一行。你只看行首,就知道某次commit的目的。

$ git log <last tag> HEAD --pretty=format:%s
fix(ng-bind-html): watch string value instead of wrapper (4 days ago)
chore(sce): remove unused function (4 days ago) <Chirayu Krishnappa>
fix(ngInclude): don't break attribute bindings on ngInclude-ed element
fix(ngView): IE8 regression due to expando on non-element nodes (4 days
docs(ngClass): fix grammar (4 days ago) <Dave Peticolas>
docs(ngCloak): fix grammar, clarity (4 days ago) <Dave Peticolas>
docs(ngModelController): clarify issue with isolated scope directive
docs(input): fix spelling error and reword for clarity(5 days ago)
doc(ngApp): fix grammar(5 days ago)<Dave Peticolas>
(origin/g3_v1_2) docs($exceptionHandler): add an example of overriding
docs(FAQ): update jQuery compatibility (5 days ago) <Pete Bacon Darwin)
docs(guide/services): rewording of explanation (5 days ago) <Pete Bacor
docs(guide/services): explain services in plain 1anguage (5 days ago)
docs(guide/i18n): change non-existent de-ge to de-de (5 days ago) <Maax
docs(ngForm): fix grammar and improve explanation (5 days ago) <Dave Pe
docs: update sticker ordering info in FAQ (6 days ago)<naomiblack>
docs(ngResource): fix typo (6 days ago) <Tim Statler>
docs(ngShowHide): fix typo (6 days ago) <Roberto Bonvallet>
docs(guide/$location): describe workaround for collocated apps (6 days
docs(tutorial/step_03):add info about karma-ng-scenario plug-in (6 day
fix(scenario): include "not "in error messages if test is inverted (6
fix($parse): disallow access to window and dom in expressions (7 days

(2)可以过滤某些commit(比如文档改动),便于快速查找信息

比如,下面的命令仅仅显示本次发布新增加的功能。

$ git log <last release> HEAD --grep feature

(3)可以直接从commit生成Change log

Change Log是发布新版本时,用来说明与上一个版本差异的文档,详见后文。

0.0.4 (2015-07-21)
Bug Fixes
·glossary: fix bad things in the glossary (9e572e9)
·intro: fix issue with the intro not working (6de0d8a)
Features
·part2: add better veggie lorem ipsum text (06912f4)
·part2: add much better lorem ipsum text (7aaf238)
BREAKING CHANGES
·Adding better veggie lorem ipsum text is great and all but it breaks all the other meaty components. Be mindful of this when you test.

二、Commit message的格式

每次提交,Commit message都包括三个部分:Header、Body和Footer。

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

其中,Header是必需的,Body和Footer可以省略。 不管是哪一个部分,任何一行都不得超过72个字符(或100个字符)。这是为了避免自动换行影响美观。

2.1 Header

Header部分只有一行,包括三个字段: type(必需)、scope(可选)和subject (必需)。

(1) type

type用于说明commit的类别,只允许使用下面7个标识。

  • feat: 新功能(feature)
  • fix: 修补bug
  • docs: 文档(documentation)
  • style:格式(不影响代码运行的变动)
  • refactor: 重构 (即不是新增功能,也不是修改bug的代码变动)
  • test:增加测试
  • chore:构建过程或辅助工具的变动

如果type为feat和fix,则该commit将肯定出现在Change log之中。其他情况(docs、chore、style、refactor、test)由你决定,要不要放入Change log,建议是不要。

(2) scope

scope用于说明commit影响的范围,比如数据层、控制层、视图层等等,视项目不同而不同。

(3) subject

subject是commit目的的简短描述,不超过50个字符。

  • 以动词开头,使用第一人称现在时,比如change,而不是changed或changes
  • 第一个字母小写
  • 结尾不加句号(.)

2.2 Body

Body部分是对本次commit的详细描述,可以分成多行。下面是一个范例。

More detailed explanatory text, if necessary. Wrap it to about 72 characters or so. Further paragraphs come after blank lines.
- Bullet points are okay, too
- Use a hanging indent

有两个注意点。

  • 使用第一人称现在时,比如使用change而不是changed或changes。
  • 应该说明代码变动的动机,以及与以前行为的对比。

2.3 Footer

Footer部分只用于两种情况。

(1)不兼容变动

如果当前代码与上一个版本不兼容,则Footer部分以BREAKING CHANGE开头,后面是对变动的描述、以及变动理由和迁移方法。

BREAKING CHANGE: isolate scope bindings definition has changed.
To migrate the code follow the example below:
Before:
scope:{
    myAttr: 'attribute',
}
After:
scope:{
    myAttr:
}
The removed inject' wasn't generally useful for directives so there should be no code using it.

(2)关闭Issue

如果当前commit针对某个issue,那么可以在Footer部分关闭这个issue。

Closes #234

也可以一次关闭多个issue。

Closes #123, #245, #992

2.4 Revert

还有一种特殊情况,如果当前commit用于撤销以前的commit,则必须以revert:开头,后面跟着被撤销Commit的Header。

revert: feat(pencil): add 'graphiteWidth' option
This reverts commit 667ecc1654a317a13331b17617d973392f415f02.

Body部分的格式是固定的,必须写成This reverts commit <hash>.,其中的hash是被撤销commit的SHA标识符。 如果当前commit与被撤销的commit,在同一个发布(release)里面,那么它们都不会出现在Change log里面。如果两者在不同的发布,那么当前commit,会出现在Change log的Reverts小标题下面。

三、Commitizen

Commitizen是一个撰写合格Commit message的工具。 安装命令如下。

$ npm install -g commitizen
$ npm install -g cz-conventional-changelog

创建初始化package.json

{
    "name": "yjauth",
    "version": "0.0.1",
    "description": "Description for yjauth",
    "private": true,
    "config":{
        "commitizen":{
            "path": "cz-conventional-changelog"
        }
    },
    "dependencies":{
    },
    "devDependencies":{
    },
    "engines":{
        "node": ">=0.12.0"
    }
}

然后,在项目目录里,运行下面的命令,使其支持Angular的Commit message格式。

$ commitizen init cz-conventional-changelog --save --save-exact

以后,凡是用到git commit命令,一律改为使用git cz。这时,就会出现选项,用来生成符合格式的Commit message。

ng-poopy master x git add.
ng-poopy master X git cz
All commit message lines will be cropped at 100 characters.
? Select the type of change that you're committing: (Use arrow keys)
> feat: A new feature
  fix: A bug fix
  docs: Documentation only changes
  style: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
  refactor: A code change that neither fixes a bug or adds a feature
  perf: A code change that improves performance
  test: Adding missing tests
  chore: Changes to the build process or auxiliary tools and libraries such as documentation generation

四、validate-commit-msg

validate-commit-msg用于检查Node项目的Commit message是否符合格式。 安装:

npm install ghooks --save-dev

接着,把这个脚本加入Git的hook。下面是在package.json里面使用ghooks,把这个脚本加为commit-msg时运行。

"config":{
    "ghooks":{
        "commit-msg": "./validate-commit-msg.js"
    }
}

然后,每次git commit的时候,这个脚本就会自动检查Commit message是否合格。如果不合格,就会报错。

$ git add -A
$ git commit -m "edit markdown" 
INVALID COMMIT MSG: does not match "<type>(<scope>): <subject>"! was: edit markdown

五、生成Change log

如果你的所有Commit都符合Angular格式,那么发布新版本时,Change log就可以用脚本自动生成(例1, 例2, 例3)。 生成的文档包括以下三个部分。

  • New features
  • Bug fixes
  • Breaking changes.

每个部分都会罗列相关的commit,并且有指向这些commit的链接。当然,生成的文档允许手动修改,所以发布前,你还可以添加其他内容。 conventional-changelog就是生成Change log的工具,运行下面的命令即可。

$ npm install -g conventional-changelog-cli
$ cd my-project
$ conventional-changelog -p angular -i CHANGELOG.md -s

This will not overwrite any previous changelog. The above generates a changelog based on commits since the last semver tag that match the pattern of a "Feature", "Fix", "Performance Improvement" or "Breaking Changes". If you first time use this tool and want to generate all previous changelog, you could do

$ conventional-changelog -p angular -i CHANGELOG.md -s -r 0

上面命令不会覆盖以前的Change log,只会在CHANGELOG.md的头部加上自从上次发布以来的变动。 如果你想生成所有发布的Change log,要改为运行下面的命令。

$ conventional-changelog -p angular -i CHANGELOG.md -w -r 0

为了方便使用,可以将其写入package.jsonscripts字段。

{
    "scripts":{
        "changelog": "conventional-changelog -p angular -i CHANGELOG.md -w -r 0"
    }
}
Git

评论

博主关闭了当前页面的评论