Skip to content

Instantly share code, notes, and snippets.

@redblue9771
Last active November 17, 2020 07:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save redblue9771/5c3cecca694ae396e906bef285b6c19b to your computer and use it in GitHub Desktop.
Save redblue9771/5c3cecca694ae396e906bef285b6c19b to your computer and use it in GitHub Desktop.
约定式提交规范

1. 全局安装commitizen & cz-conventional-changelog

commitizen是一个撰写合格commit message的工具,用于代替git commit指令,而 cz-conventional-changelog 适配器提供 conventional-changelog 标准(约定式提交标准)。

基于不同需求,也可以使用不同适配器。

npm install -g commitizen cz-conventional-changelog
echo '{ "path": "cz-conventional-changelog" }' > ~/.czrc

安装完毕后,可直接使用git cz来取代git commit。 全局模式下,需要~/.czrc配置文件, 为 commitizen 指定 Adapter。

2. 项目内安装commitlint & husky

commitlint 负责用于对 commit message 进行格式校验,husky 负责提供更易用的 git hook。

  • Use npm
npm i -D husky @commitlint/config-conventional @commitlint/cli
  • Use yarn
yarn add husky @commitlint/config-conventional @commitlint/cli -D

commitlint只能做格式规范,无法触及内容。对于内容质量的把控只能靠我们自己。

3. 添加相应配置

创建commitlint.config.js

  1. In the same path as package.json
echo 'module.exports = {extends: ["@commitlint/config-conventional"]};' > ./commitlint.config.js
  1. 引入husky
  • package.json
...,
"husky": {
    "hooks": {
      "commit-msg": "commitlint -e $GIT_PARAMS"
    }
}

4. 使用

执行git cz进入 interactive 模式,根据提示依次填写:

1.Select the type of change that you're committing 选择改动类型 (<type>)

2.What is the scope of this change (e.g. component or file name)? 填写改动范围 (<scope>)

3.Write a short, imperative tense description of the change: 写一个精简的描述 (<subject>)

4.Provide a longer description of the change: (press enter to skip) 对于改动写一段长描述 (<body>)

5.Are there any breaking changes? (y/n) 是破坏性修改吗?默认n (<footer>)

6.Does this change affect any openreve issues? (y/n) 改动修复了哪个问题?默认n (<footer>)

生成的commit message格式如下:

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

填写完毕后,husky 会调用 commitlint 对 message 进行格式校验,默认规定 type 及 subject 为必填项。

任何 git commit 指令的 option 都能用在 git cz 指令上, 例如git cz -a


大牛总结的 Git 使用技巧,这篇推荐大家看下。

  1. type type为必填项,用于指定commit的类型,约定了feat、fix两个主要type,以及docs、style、build、refactor、revert五个特殊type,其余type暂不使用。
  • 主要type

feat: 增加新功能 fix: 修复bug

  • 特殊type

docs: 只改动了文档相关的内容 style: 不影响代码含义的改动,例如去掉空格、改变缩进、增删分号 build: 构造工具的或者外部依赖的改动,例如webpack,npm refactor: 代码重构时使用 revert: 执行git revert打印的message

  • 暂不使用type

test: 添加测试或者修改现有测试 perf: 提高性能的改动 ci: 与CI(持续集成服务)有关的改动 chore: 不修改src或者test的其余修改,例如构建过程或辅助工具的变动

当一次改动包括主要type与特殊type时,统一采用主要type。Git 的这个神技,学会爽歪歪~这篇推荐阅读。 2. scope scope也为必填项,用于描述改动的范围,格式为项目名/模块名,例如: node-pc/common rrd-h5/activity,而we-sdk不需指定模块名。如果一次commit修改多个模块,建议拆分成多次commit,以便更好追踪和维护。

  1. body body填写详细描述,主要描述改动之前的情况及修改动机,对于小的修改不作要求,但是重大需求、更新等必须添加body来作说明。

  2. break changes break changes指明是否产生了破坏性修改,涉及break changes的改动必须指明该项,类似版本升级、接口参数减少、接口删除、迁移等。

  3. affect issues affect issues指明是否影响了某个问题。例如我们使用jira时,我们在commit message中可以填写其影响的JIRA_ID,若要开启该功能需要先打通jira与gitlab

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment