把项目的git commit message规范化,对于项目的合作开发、发布等都有很大帮助。同时还能自动生成语义化的CHANGE-LOG。我们可以参考具体的标准规范化自己的提交,也可以使用辅助工具进行强制性规范。

文中使用到的工具清单:

  • commitizen: 替代git commit
  • cz-conventional-changelog: 符合angular规范的adapter
  • commitlint: 校验git message
  • husky: git hook 工具

Angular 团队规范

message 格式如下:

<type>(<scope>): <subject>
<BLANK LINE>
<body>
<BLANK LINE>
<footer>

Message 主要分为三个部分:

  • 标题行: 必填, 描述主要修改类型和内容
  • 主题内容: 描述为什么修改, 做了什么样的修改, 以及开发的思路等等
  • 页脚注释: 放 Breaking Changes 或 Closed Issues 分别由如下部分构成:
  • type: commit 的类型
  • feat: 新特性
  • fix: 修改问题
  • refactor: 代码重构
  • docs: 文档修改
  • style: 代码格式修改, 注意不是 css 修改
  • test: 测试用例修改
  • chore: 其他修改, 比如构建流程, 依赖管理.
  • scope: commit 影响的范围, 比如: route, component, utils, build…
  • subject: commit 的概述, 建议符合 50/72 formatting
  • body: commit 具体修改内容, 可以分为多行, 建议符合 50/72 formatting
  • footer: 一些备注, 通常是 BREAKING CHANGE 或修复的 bug 的链接.

git commit 模板 我们通过修改 git 配置,指定 message 模板具体配置参考https://www.git-scm.com/book/zh/v2/%E8%87%AA%E5%AE%9A%E4%B9%89-Git-%E9%85%8D%E7%BD%AE-Git

message 模板:

# head: <type>(<scope>): <subject>
# - type: feat, fix, docs, style, refactor, test, chore
# - scope: can be empty (eg. if the change is a global or difficult to assign to a single component)
# - subject: start with verb (such as 'change'), 50-character line
#
# body: 72-character wrapped. This should answer:
# * Why was this change necessary?
# * How does it address the problem?
# * Are there any side effects?
#
# footer:
# - Include a link to the ticket, if any.
# - BREAKING CHANGE
#

使用Commitizen

下面介绍使用工具规范化 commit 的详细步骤。

提交工具 使用commitzen工具替代git commit,它能通过简单的交互操作,将 message 从文本编辑方式转换成简单的交互操作

名称:Commitizen
安装:npm install —save-dev commitizen
仓库地址:https://github.com/commitizen/cz-cli

Adapter Adapter 是具体的规范标准配置,我们可以使用一个符合 angular 规范的 adapter:cz-conventional-changelog,也可以自定义自己的规范

名称:cz-conventional-changelog
安装:npm install —save-dev cz-conventional-changelog
仓库地址:https://link.juejin.im/?target=https%3A%2F%2Flink.zhihu.com%2F%3Ftarget%3Dhttps%253A%2F%2Fgithub.com%2Fcommitizen%2Fcz-conventional-changelog

配置 安装好commitzencz-conventional-changelog之后,还需要添加一些配置:

package.json 中添加:

"scripts": {
  "commit": "git-cz"
},
"config": {
  "commitizen": {
    "path": "node_modules/cz-conventional-changelog"
  }
}

在配置中,我们需要指定 Adapter,否则在执行git-cz的时候,不会出现选择的交互界面,而是直接弹出 vim 编辑器编辑 message。

配置好之后使用npm run commit替代git commit提交。

对提交进行验证

commitzen只是帮助我们规范化commit message,但是如果没有按规范提交,也是会成功的。所以我们需要一个工具进行检测,不安规范的提交全部终止。

我们可以使用commitlint对提交进行验证,同时配合git hook,可以在提交的时候验证,验证不通过会自动终止提交操作。同时,我们需要一份校验的配置,告诉 commitlint 校验规则,我们使用符合 angular 规范的一份配置: @commitlint/config-conventionalgit hook可以使用Husky来进行边便捷的配置。

安装插件: npm install --save-dev commitlint @commitlint/config-conventional husky

配置: 在项目目录添加配置文件commitlint.config.js,配置内容如下:

module.exports = {
  extends: ['@commitlint/config-conventional'],
}

然后在package.json中添加配置:

"husky": {
  "hooks": {
  "commit-msg": "commitlint -e $GIT_PARAMS"
  }
}

配置完成后即可。

生成 CHANGELOG

我们可以使用standard-version来自动生成 CHANGELOG,使用方法很简单,安装插件后,执行npx standard-version即可在项目目录下生成CHANGELOG.md文件。

参考文章:

  1. 优雅的提交你的 Git Commit Message
  2. Git - 配置 Git