前言
Git每次提交代码,都要写Commit message(提交说明),否则就不允许提交。一般来说,commit message应该清晰明了,说明本次提交的目的。
不过,在团队合作中,为了保持每个开发者的提交信息能够让所有人一目了然,达到共识,有规范和工具的约束是必然的选择。
写法规范
目前,commit message有多重写法规范。本文介绍Angular规范是目前使用比较广泛的写法,比较系统化,并且有配套的工具。
这里不推荐使用git commit -m
,单行显示的提交信息量太少。
这里,我们使用以下提示信息作为commit模板,用以在提交时做格式提示。
# Format of the commit message
# <type>(<scope>): <subject>
# <BLANK LINE>
# <body>
# <BLANK LINE>
# <footer>
#
# <type>
# * feat (feature)(新功能)
# * fix (bug fix)(修补bug)
# * docs (documentation)(文档)
# * style (formatting, missing semi colons, …)(格式,不影响代码运行的变动)
# * refactor(重构,即不是新增功能,也不是修改bug的代码变动)
# * test (when adding missing tests)(增加测试)
# * chore (maintain)(构建过程或辅助工具的变动)
# <type>
# <scope>
# Scope can be anything specifying place of the commit change.
# 用于说明commit影响的范围,比如数据层、控制层、视图层等。
# <scope>
# <subject>
# This is a very short description of the change.
# 用于本次commit的简短描述,不超过50个字符。
# <subject>
# <body>
# 本次commit的详细描述,可以分成多行书写
# <body>
# <footer>
# 用来关闭Issue或以BREAKING开头,后面是对变动的描述、记忆变动理由和迁移方法。
# <footer>
-
打开
git bash
-
touch ~/.git-commit-template
-
vim ~/.git-commit-template
-
拷贝上述文本并保存退出
-
git config --global commit.template ~/.git-commit-template
-
使用
git commit -s
会有模本提示
Commit message的作用
提供更多的历史信息,方便自己和其它开发者快速浏览
通过git log <last tag> --pretty=format:%s
,可以单行显示每个commit,只看行首,就可以知道某次提交的目的。
过滤某些commit(比如文档改动),便于快速查找信息
通过git log <last release> HEAD --grep feature
,可以仅仅显示本次发布新增加的功能。
可以直接从commit生成Change log
Change log是在发布新版本时,用来与上一个版本差异的文档。