一、Commit message 的作用
格式化的Commit message,有几个好处。
(1)提供更多的历史信息,方便快速浏览。
比如,下面的命令显示上次发布后的变动,每个commit占据一行。你只看行首,就知道某次 commit 的目的。
$ git log HEAD –pretty=format:%s
(2) 可以过滤某些commit(比如文档改动),便于快速查找信息。
$ git log HEAD –grep feature
(3)可以直接从commit生成Change log。
二、Commit message 的格式
<type>(<scope>): <subject>
// 空一行
<body>
// 空一行
<footer>
|
其中,Header 是必需的,Body 和 Footer 可以省略。
不管是哪一个部分,任何一行都不得超过72个字符(或100个字符)。这是为了避免自动换行影响美观。
2.1 Header
type
(必需)、
scope
(可选)和
subject
(必需)。
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,建议是不要。
scope
用于说明 commit 影响的范围,比如数据层、控制层、视图层等等,视项目不同而不同。
subject
是 commit 目的的简短描述,不超过50个字符。
以动词开头,使用第一人称现在时,比如change,而不是changed或changes
第一个字母小写
结尾不加句号(.)
2.2 Body
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
有两个注意点。
(1)使用第一人称现在时,比如使用change
而不是changed
或changes
。
(2)应该说明代码变动的动机,以及与以前行为的对比。
2.3 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 generaly useful for directives so there should be no code using it.
Closes #234
或
Closes #123, #245, #992
2.4 Revert
revert:
开头,后面跟着被撤销 Commit 的 Header。
revert: feat(pencil): add ‘graphiteWidth’ option
This reverts commit 667ecc1654a317a13331b17617d973392f415f02.
This reverts commit <hash>.
,其中的
hash
是被撤销 commit 的 SHA 标识符。
Reverts
小标题下面。
三、Commitizen
$ npm install -g commitizen
$ npm init
$ commitizen init cz-conventional-changelog –save –save-exact
git commit
命令,一律改为使用
git cz
。这时,就会出现选项,用来生成符合格式的 Commit message。
四、生成 C hange log
New features
Bug fixes
Breaking changes.
$ npm install -g conventional-changelog
$ cd my-project
$ conventional-changelog -p angular -i CHANGELOG.md -w
CHANGELOG.md
的头部加上自从上次发布以来的变动。