Git-Commit-Message

Gitcommit时,必须输入commit message,来表示对提交内容的说明。没有则无法提交。

理论上,commit message可以输入任意内容。但如果参与多人协作开发和持续开发,就会有相应的规范,帮助程序员快速理解别人写的提交,或者回顾自己以前提交的作用。

目前应用最为广泛的规范是前端框架Angular提出的Angular提交信息规范(简称规范)。

Angular Commit Message Format

总的来说,规范要求,每次提交信息包含三部分。

1.信息头(header)

2.信息体(body)

3.信息尾(footer)

三个部分用空行间隔,来区分。其中bodyfooter为可选部分。

Message
1
2
3
4
5
<header>
<BLANK LINE>
<body>
<BLANK LINE>
<footer>

header是三个部分中最复杂的。包含三个区域<type><scope><short summary>

1
2
3
4
5
6
7
8
9
10
11
<type>(<scope>): <short summary>
│ │ │
│ │ └─⫸ Summary in present tense. Not capitalized. No period at the end.
│ │
│ └─⫸ Commit Scope: animations|bazel|benchpress|common|compiler|compiler-cli|core|
│ elements|forms|http|language-service|localize|platform-browser|
│ platform-browser-dynamic|platform-server|router|service-worker|
│ upgrade|zone.js|packaging|changelog|docs-infra|migrations|
│ devtools

└─⫸ Commit Type: build|ci|docs|feat|fix|perf|refactor|test

其中,<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

其中用的最多的应该是featfixtest

<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历史记录时,可以选择只显示第一行内容。可以很快地理解每次提交的性质。

commit-log

body

这一部分详细描述提交的内容,比如所做修改的动机,所做修改的思路,改动前后程序表现的对比。

规范相对于header来说很简单,仅仅是使用祈使句和现在时。

这一部分一般是使用Github时才会较多使用,主要用来引用提交所针对得PRissue,另外也可以用来说明重大更改和弃用说明。

由于这一部分在公司内使用不多,大部分情况下为空即可,因此不过多介绍,详见官方文档

总结

使用git commit时需要输入commit message,目前最常用的规范是AngularJS commit message format

此规范包含三部分,其中最重要的部分为headerheader中需要指明修改的类型、影响的范围和简单的一句话概括。


Git-Commit-Message
http://dracoyus.github.io/2023/05/12/Git-Commit-Message/
作者
DracoYu
发布于
2023年5月12日
许可协议