版本控制系统:Commit messages 的基本语法

无论是个人项目还是在团队协作中,commit message 都应该清晰明了,遵守一定规范。
目前,社区有多种 commit message 的写法规范。本文介绍 Angular规范,这是目前使用最广的写法,比较合理和系统化,并且有配套的工具。

当前业界应用的比较广泛的是 Angular Git Commit Guidelines

一、格式化 commit message 的目的

  • 提供更多的历史信息,方便快速浏览。
  • 可以过滤某些 commit(比如文档改动),便于快速查找信息。
  • 可以直接从 commit 生成 Change log。

二、commit message 的格式

每次提交,commit message 都包括三个部分:Header,Body 和 Footer。

具体格式为:

<type>(<scope>): <subject>
空一行
<body>
空一行
<footer>

1. Header

Header部分只有一行,包括三个字段:type(必需)、scope(可选)和subject(必需)。

(1) type

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,建议是不要。

(2) scope

用于说明 commit 影响的范围,比如数据层、控制层、视图层等等,视项目不同而不同。

(3) subject

subject 是 commit 目的的简短描述,不超过 50 个字符。

  • 以动词开头,使用第一人称现在时,比如 change,而不是 changed 或 changes。
  • 第一个字母小写。
  • 结尾不加句号(.)。

2. Body

用于对本次 commit 的详细描述,可以分成多行。下面是一个范例:

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) 应该说明代码变动的动机,以及与以前行为的对比。

3. Footer

Footer 部分只用于两种情况。

(1) 不兼容变动

如果当前代码与上一个版本不兼容,则 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.

(2) 关闭 Issue

如果当前 commit 针对某个issue,那么可以在 Footer 部分关闭这个 issue。


Closes #234

也可以一次关闭多个 issue。


Closes #123, #245, #992

4. Revert(可忽视)

还有一种特殊情况,如果当前 commit 用于撤销以前的 commit,则必须以 revert: 开头,后面跟着被撤销 Commit 的 Header。
bash

revert: feat(pencil): add 'graphiteWidth' option

This reverts commit 667ecc1654a317a13331b17617d973392f415f02.

Body 部分的格式是固定的,必须写成 This reverts commit <hash>.,其中的 hash 是被撤销 commit 的 SHA 标识符。

如果当前 commit 与被撤销的 commit,在同一个发布(release)里面,那么它们都不会出现在 Change log 里面。如果两者在不同的发布,那么当前 commit,会出现在 Change log 的Reverts小标题下面。

Commit messages 格式要求

复制代码
# 标题行:50个字符以内,描述主要变更内容
#
# 主体内容:更详细的说明文本,建议72个字符以内。 需要描述的信息包括:
#
# * 为什么这个变更是必须的? 它可能是用来修复一个bug,增加一个feature,提升性能、可靠性、稳定性等等
# * 他如何解决这个问题? 具体描述解决问题的步骤
# * 是否存在副作用、风险?
#
# 如果需要的化可以添加一个链接到issue地址或者其它文档
复制代码

具体案例1

feat($browser): onUrlChange event (popstate/hashchange/polling)
  
Added new event to $browser:
- forward popstate event if available
- forward hashchange event if popstate not available
- do polling when neither popstate nor hashchange available
  
Breaks $browser.onHashChange, which was removed (use onUrlChange instead)

具体案例2

fix($compile): couple of unit tests for IE9
  
Older IEs serialize html uppercased, but IE9 does not...
Would be better to expect case insensitive, unfortunately jasmine does
not allow to user regexps for throw expectations.
  
Closes #392
Breaks foo.bar api, foo.baz should be used instead

参考文章:

https://xie.infoq.cn/article/dffa8c4efd68796bc526639ee  Git 提交信息规范化

https://www.cnblogs.com/fei8899/p/14464877.html Git 提交信息规范

posted @   培轩  阅读(138)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 推荐几款开源且免费的 .NET MAUI 组件库
· 实操Deepseek接入个人知识库
· 易语言 —— 开山篇
· Trae初体验
点击右上角即可分享
微信分享提示