Fantetic Blog

Git提交规范

约束是一种良好的实践。

在 github 上逛逛就可以发现,其提交的 commit 都有一定格式,工作中也有相应的规定,时间长了就能体会到其中的好处。这种约束是一种良好的实践。抽出一些时间,更详细的了解相关的资料,然后做了一些实践和总结。

规范 Commit 的好处

  1. 提供更明确的历史信息,方便判断提交目的和浏览
  2. 可以过滤某些不必要的提交,方便快速查找信息
  3. 自动化生成 Changelog
  4. 向同事、公众与其他利益关系人传达变化的性质
  5. 基于提交的类型,自动决定语义化的版本变更

以上的好处,个人认为要有一个大的前提,就是每一个提交,尽量保证其目的单一性,比如说几个 bug 看似类似,就一次性修改提交。这样做,让 commit 的信息变的复杂化,阅读不方便,也容易让人想到一些不必要的关联性。

Commit 的格式

一个规范的Git提交描述格式包括如下:

# Header头
<type>(<scope>): <subject>

# Body体
<body>

# Footer体
<footer>

Type 提交类型

type 用于说明 commit 的类别,必须为以下类型的一种:

  • feat: 新的功能
  • fix: 修复 bug
  • docs: 只是文档的更改
  • style: 不影响代码含义的更改 (例如空格、格式化、少了分号)
  • refactor: 既不是修复 bug 也不是添加新功能的代码更改,重构
  • perf: 提高性能的代码更改
  • test: 添加或修正测试
  • chore: 对构建或者辅助工具的更改,或者增加依赖库
  • build: 构建系统

Scope 作用范围

scope 用于说明 commit 影响的范围,当影响的范围有多个时候,可以使用 *。一般是修改的什么模块或者是什么功能,如【xx模块】/【xx功能】

Subject 提交主题

subject 用于对 commit 变化的简洁描述:

  • 一般是5-10各自简单描述做的任务,如【xx模块加入消息队列】
  • 使用祈使句,一般以动词原形开始,例如使用 change 而不是 changed 或者 changes
  • 第一个字母小写
  • 结尾不加句号(.)

Body 体

body 用于对 commit 详细描述。

  • 对于功能详细的描述,解释为什么加入这段代码,为什么调整优化等,如因分布式锁问题,导致死锁问题,优化调整xxxx。
  • 使用祈使句,一般以动词原形开始,例如使用 change 而不是 changed 或者 changes。
  • body 应该包含这次变化的动机以及与之前行为的对比。

footer 目前用于两种情况:

1 不兼容的变动

所有不兼容的变动都必须在 footer 区域进行说明,以 BREAKING CHANGE: 开头,后面的是对变动的描述,变动的理由和迁移注释。

BREAKING CHANGE: isolate scope bindings definition has changed and
    the inject option for the directive controller injection was removed.

    To migrate the code follow the example below:

    Before:

    scope: {
      myAttr: 'attribute',
      myBind: 'bind',
      myExpression: 'expression',
      myEval: 'evaluate',
      myAccessor: 'accessor'
    }

    After:

    scope: {
      myAttr: '@',
      myBind: '@',
      myExpression: '&',
      // myEval - usually not useful, but in cases where the expression is assignable, you can use '='
      myAccessor: '=' // in directive's template change myAccessor() to myAccessor
    }

 The removed `inject` wasn't generaly useful for directives so there should be no code using it.

2 关闭 issue

当前 commit提交针对某个issue问题或者是禅道bug编号等,如Closes # 234

## 关闭单个
Closes #234
## 关闭多个
Closes #123, #245, #992

示例

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)
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 #351
style($location): add couple of missing semi colons
docs(guide): updated fixed docs from Google Docs

Couple of typos fixed:
- indentation
- batchLogbatchLog -> batchLog
- start periodic checking
- missing brace

Commit 相关的工具

填写提示工具 commitizen

这个工具是用来给 commit 一个引导的作用,根据提示一步一步的完善 commit。

idea插件:【git commit message helper】

2024-03-22T01:14.png

感受

网上有很多类似的介绍,自己动手去实践,得到的比看到的多的多。

参考资料

Git