You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
base-project/document/Git commit 规范.md

3.9 KiB

简介

实际开发过程中少不了使用Git进行代码版本管理每次代码提交时都需要写Commit message此时规范就相当重要了业内做的比较好的比较具有参考价值的就是Angular的提交。


Commit message 的格式

Angular提交规范:

每次提交Commit message 都包括三个部分headerbody 和 footer。

<type>(<scope>): <subject> #header
// 空一行
<body>
// 空一行
<footer> 
12345

其中header 是必需的body 和 footer 可以省略。 不管是哪一个部分任何一行都不得超过72个字符或100个字符。这是为了避免自动换行影响美观。

Header

Header部分只有一行包括三个字段type必需scope可选和subject必需

type

用于说明 commit 的类别只允许使用下面7个标识。

  • feat新功能feature
  • fix修补bug
  • docs文档documentation
  • style 格式(不影响代码运行的变动)
  • refactor重构即不是新增功能也不是修改bug的代码变动
  • test增加测试
  • chore构建过程或辅助工具的变动

如果type为featfix,则该 commit 将肯定出现在 Change log 之中。其他情况(docs、chore、style、refactor、test)由你决定,要不要放入 Change log建议是不要。

scope

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

例如在Angular,可以是$location, $browser, $compile, $rootScope, ngHref, ngClick, ngView等。

如果你的修改影响了不止一个scope你可以使用*代替。

subject

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

其他注意事项:

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

Body

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
1234567

有两个注意点:

  • 使用第一人称现在时比如使用change而不是changed或changes。
  • 永远别忘了第2行是空行
  • 应该说明代码变动的动机,以及与以前行为的对比。

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.
1234567891011121314151617
  1. 关闭 Issue 如果当前 commit 针对某个issue那么可以在 Footer 部分关闭这个 issue 。
Closes #234

Revert

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

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

This reverts commit 667ecc1654a317a13331b17617d973392f415f02.
123

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

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