规范git commit提交信息

本文参考:https://www.jianshu.com/p/8efa36c5dfd4
https://segmentfault.com/a/1190000017790694

一、commitlint+husky

1、安装commitlint

安装

npm install --save-dev @commitlint/config-conventional @commitlint/cli

在目录添加配置文件commitlint.config.cjs

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

2、安装husky

安装

npm install --save-dev husky

若husky版本<5.0.0:

在package.json中配置:

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

若husky版本>=5.0.0:

  1. 执行 npx husky install 安装git钩子 (运行后会在根目录创建一个.husky文件夹)
  2. 执行 npx husky add .husky/commit-msg 'npx --no-install commitlint --edit $1' 启用适配commitlint的commit-msg hook(注意这里使用的是单引号而不是双引号)

3、可定制提交规范

提交格式(注意冒号后面有空格)

: 

常用的type类别

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

例子:

git commit -m 'feat: 增加 xxx 功能'
git commit -m 'bug: 修复 xxx 功能'

subject

subject是 commit 目的的简短描述,可以做一些配置,如最大长度限制。

commitlint.config.cjs文件配置

rule配置说明::rule由name和配置数组组成,如:'name:[0, 'always', 72]',数组中第一位为level,可选0,1,2,0为disable,1为warning,2为error,第二位为应用与否,可选always|never,第三位该rule的值。具体配置例子如下:

module.exports = {
  extends: [
    "@commitlint/config-conventional"
  ],
  rules: {
    'type-enum': [2, 'always', [
      'upd', 'feat', 'fix', 'refactor', 'docs', 'chore', 'style', 'revert'
     ]],
    'type-case': [0],
    'type-empty': [0],
    'scope-empty': [0],
    'scope-case': [0],
    'subject-full-stop': [0, 'never'],
    'subject-case': [0, 'never'],
    'header-max-length': [0, 'always', 72]
  }
};

配置后提交

git commit -m '新增登陆页面'  // 报错,type-empty不能为空
git commit -m 'feat: 新增登陆页面'  // 正确

错误提交:

PS G:\demo\vue3-demo> git add .
PS G:\demo\vue3-demo> git commit -m '错误提交'
⧗   input: 错误提交
✖   subject may not be empty [subject-empty]

✖   found 1 problems, 0 warnings
ⓘ   Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint

husky - commit-msg hook exited with code 1 (error)

二、Commitizen

安装

$ npm install -g commitizen

在项目目录里运行下面命令

$ commitizen init cz-conventional-changelog --save --save-exact

以后,凡是用到git commit命令,一律改为使用git cz。这时,就会出现选项,用来生成符合格式的 Commit message。

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

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

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

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

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

5.Are there any breaking changes? (y/n) 是破坏性修改吗?默认n (
) 6.Does this change affect any openreve issues? (y/n) 改动修复了哪个问题?默认n (
)

你可能感兴趣的:(规范git commit提交信息)