Git提交消息

2017-10-30  本文已影响0人  HelenYin
如何给git commit 添加一段简短且有意义的描述

每一个commit应该包括以下几部分:header,body,footer
header包括type,scope,subject

<type>(<scope>): <subject>
<BLANK LINE>
<body>
<BLANK LINE>
<footer>

type

type是以下的几个:

Scope

scope是可选的,用来说明本次提交影响的范围(在angular的代码提交中会写这些$location, $browser, $compile, $rootScope, ngHref, ngClick, ngView

Subject

Subject最多50个字

Body


并不是所有的commit都需要body,只有当这个commit需要一些说明的时候才添加body。有时候一行Subject来说明足以。body中备注代码改动的什么,以及为什么要这样改(而不是怎么改的)

下面是一个示范:

commit eb0b56b19017ab5c16c745e6da39c53126924ed6
Author: Pieter Wuille <pieter.wuille@gmail.com>
Date:   Fri Aug 1 22:57:55 2014 +0200

   Simplify serialize.h's exception handling

   Remove the 'state' and 'exceptmask' from serialize.h's stream
   implementations, as well as related methods.

   As exceptmask always included 'failbit', and setstate was always
   called with bits = failbit, all it did was immediately raise an
   exception. Get rid of those variables, and replace the setstate
   with direct exception throwing (which also removes some dead
   code).

   As a result, good() is never reached after a failure (there are
   only 2 calls, one of which is in tests), and can just be replaced
   by !eof().

   fail(), clear(n) and exceptions() are just never called. Delete
   them.

需要注意:在写body的时候,保证每行最多只有72个字符

Footer


footer是可选的,可以用来跟踪issue的id,比如这个commit是用来关闭了某个issue:

Closes #123

Commitizen


可以使用commitizen来帮助团队规范化整个流程,与npm scripts配合自动化整个流程。

首先,本地安装Commitizen

npm install commitizen --save-dev

然后安装cz-conventional-changelogcz-conventional-changelog是一个适配器,用来适配不同代码的提交。这里我们项目中使用的是angular的代码适配器

npm install cz-conventional-changelog --save-dev

然后在package.json的root添加

"config": {
    "commitizen": {
      "path": "cz-conventional-changelog"
    }
  }

现在便可以使用Commitizen来提交git commit,具体用法就是,在git commit的时候改为使用git-cz.这里我把命令写进npm script中

"scripts": {
    "commit": "git-cz"
  }

然后我们在提交代码时先git add -A,然后npm run commit,就会在命令行提示输入header(type, scope, subject), body, footer

上一篇下一篇

猜你喜欢

热点阅读