Git-Commit-Message
Git
在commit
时,必须输入commit message
,来表示对提交内容的说明。没有则无法提交。
理论上,commit message
可以输入任意内容。但如果参与多人协作开发和持续开发,就会有相应的规范,帮助程序员快速理解别人写的提交,或者回顾自己以前提交的作用。
目前应用最为广泛的规范是前端框架Angular
提出的Angular提交信息规范(简称规范)。
Angular Commit Message Format
总的来说,规范要求,每次提交信息包含三部分。
1.信息头(header
)
2.信息体(body
)
3.信息尾(footer
)
三个部分用空行间隔,来区分。其中body
和footer
为可选部分。
1 |
|
header
header是三个部分中最复杂的。包含三个区域<type>
、<scope>
和<short summary>
。
1 |
|
其中,<type>
为必须项,所有可能的取值和意义如下。
- build: Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm)
- ci: Changes to our CI configuration files and scripts (examples: CircleCi, SauceLabs)
- docs: Documentation only changes
- feat: A new feature
- fix: A bug fix
- perf: A code change that improves performance
- refactor: A code change that neither fixes a bug nor adds a feature
- test: Adding missing tests or correcting existing tests
其中用的最多的应该是feat
、fix
和test
。
<scope>
为可选项,可能的取值人为规定,一般使用项目的某个模块名或业务名,用来表示提交的修改的影响范围。
<short summary>
为必须项。用一句话概括此次提交。此部分的要求如下。
- use the imperative, present tense: "change" not "changed" nor "changes"
- don't capitalize the first letter
- no dot (.) at the end
这样子设置header的好处是,在查看commit
历史记录时,可以选择只显示第一行内容。可以很快地理解每次提交的性质。

body
这一部分详细描述提交的内容,比如所做修改的动机,所做修改的思路,改动前后程序表现的对比。
规范相对于header
来说很简单,仅仅是使用祈使句和现在时。
footer
这一部分一般是使用Github时才会较多使用,主要用来引用提交所针对得PR
或issue
,另外也可以用来说明重大更改和弃用说明。
由于这一部分在公司内使用不多,大部分情况下为空即可,因此不过多介绍,详见官方文档。
总结
使用git commit
时需要输入commit message
,目前最常用的规范是AngularJS commit message format
。
此规范包含三部分,其中最重要的部分为header
。header
中需要指明修改的类型、影响的范围和简单的一句话概括。