Skip to content

git 提交信息规范

仲灏2022-02-24约 1 分钟

<div style="display: none;" hidden="true" aria-hidden="true" data-nosnippet>Are you an LLM? You can read better optimized documentation at /pages/2b68bd.md for this page in Markdown format</div>

前言

git commit -m "" 一千个人,就有一千条信息,可谓是五花八门,天马行空,博大精深。。。

规范内容

下面介绍的是用的比较多的一种提交规范,Google下 Angular团队规范延伸出的约定式提交(Conventional Commits specification)

https://github.com/angular/angular/blob/22b96b9/CONTRIBUTING.md#-commit-message-guidelines

https://docs.google.com/document/d/1QrDFcIiPjSLDn3EL15IJygNPiHORgU1_OOAqWjiDU5Y/edit#

Commit message的格式约定的规则总结如下:

位置是否必须实例
Header必需
type必需feat: 新功能<br/>fix: 错误修复<br/>docs: 仅文档更改<br/>style: 不影响代码含义的更改<br/> (空格、格式、缺少分号等)<br/>refactor: 既不修复 Bug 也不添加功能的代码更改<br/>perf: 提高性能的代码更改<br/>test: 添加缺少的测试<br/>chore: 对生成过程或辅助工具的更改<br/> 和库如文档生成<br/>revert: 还原到提交<br/>WIP: 进行中工作'
scope可选$location, $browser, $compile, $rootScope, ngHref, ngClick, ngView, etc...
subject必需
Body可选
Footer可选

翻译如下

git
<类型>[可选 范围]: <描述>

[可选 正文]

[可选 脚注]

可以得出简洁版:<类型>: <描述>

举例:

  • 简洁版

docs: 修改了登录授权的文档

  • 详细版
git
docs[登录授权]: 修改了登录授权的文档

将文档中的企业授权改为公司授权

第18个ISSUE

辅助规范插件

Commitizen

使用 Commitizen 提交时,系统将提示您在提交时填写任何必需的提交字段。

GitHub:https://github.com/commitizen/cz-cli

使用

  • 安装 ( 我的是commitizen@4.2.4 )
shell
npm install -g commitizen@4.2.4

cz-customizable

安装

npm i cz-customizable@6.3.0 -D

配置

json
...
"config": {
  "commitizen": { // not needed for standlone usage
    "path": "node_modules/cz-customizable"
  }
}
  • 在您的主目录中创建一个名为 .cz-config.js 的文件(以下内容为我逐行翻译)

    • js
      module.exports = {
        types: [
          { value: 'feat', name: 'feat:     新功能' },
          { value: 'fix', name: 'fix:      错误修复' },
          { value: 'docs', name: 'docs:     仅文档更改' },
          {
            value: 'style',
            name: 'style:    不影响代码含义的更改\n            (空格、格式、缺少分号等)'
          },
          {
            value: 'refactor',
            name: 'refactor: 既不修复 Bug 也不添加功能的代码更改'
          },
          {
            value: 'perf',
            name: 'perf:     提高性能的代码更改'
          },
          { value: 'test', name: 'test:     添加缺少的测试' },
          {
            value: 'chore',
            name: 'chore:    对生成过程或辅助工具的更改\n            和库,如文档生成'
          },
          { value: 'revert', name: 'revert:   还原到提交' },
          { value: 'WIP', name: 'WIP:      进行中工作' }
        ],
        messages: {
          type: "选择要提交的更改类型:",
          scope: '\n表示此更改的范围(可选):',
          // used if allowCustomScopes is true
          customScope: '表示此更改的范围:',
          subject: '写一个简短的, 命令式的更改时态描述:\n',
          body: '提供更改的较长描述(可选)。使用"|"换行:\n',
          breaking: '列出所有重大更改(可选):\n',
          footer: '列出此更改解决的任何问题(可选)。例如:31、#34:\n',
          confirmCommit: '是否确实要继续执行上述提交?'
        }
      }

使用

  • 保存git add .

  • 提交代码: git cz 即可看到下方规范的commit message 各个部位的选择及其提示,如图:

    • image-20220225104937905
  • 做类似例子处理,如:

    • image-20220225105714744
  • 在git提交记录中也以看到:

    • image-20220225105911079

限制只能通过git cz指令提交

请看另一篇文章 git hooks