说明: 项目中需要约定提交信息规范,可使用husky和commitlint,对git的commit信息进行校验。

安装

  1. yarn add husky --dev
  2. yarn add pinst --dev # ONLY if your package is not private


开启Git Hooks

  1. yarn husky install


要在安装后自动启用 Git Hooks,请编辑 package.json

  1. // package.json
  2. {
  3. "private": true, // your package is private, you only need postinstall
  4. "scripts": {
  5. "postinstall": "husky install"
  6. }
  7. }

postinstall会在你yarn安装时自动执行

Hooks

  1. # 命令创建hooks
  2. npx husky add .husky/[gitHooks] [content]

commit-msg

  1. # $1 .git/COMMIT_EDITMSG
  2. npx husky add .husky/commit-msg npx --no-install commitlint --edit $1

创建commitlint.config.js 对commit校验

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

可扩展自定义规则,@commitlint/config-conventional默认的规则如下:

  1. rules: {
  2. 'body-leading-blank': [1, 'always'],
  3. 'body-max-line-length': [2, 'always', 100],
  4. 'footer-leading-blank': [1, 'always'],
  5. 'footer-max-line-length': [2, 'always', 100],
  6. 'header-max-length': [2, 'always', 100],
  7. 'subject-case': [
  8. 2,
  9. 'never',
  10. ['sentence-case', 'start-case', 'pascal-case', 'upper-case'],
  11. ],
  12. 'subject-empty': [2, 'never'],
  13. 'subject-full-stop': [2, 'never', '.'],
  14. 'type-case': [2, 'always', 'lower-case'],
  15. 'type-empty': [2, 'never'],
  16. 'type-enum': [
  17. 2,
  18. 'always',
  19. [
  20. 'build',
  21. 'chore',
  22. 'ci',
  23. 'docs',
  24. 'feat',
  25. 'fix',
  26. 'perf',
  27. 'refactor',
  28. 'revert',
  29. 'style',
  30. 'test',
  31. ],
  32. ],
  33. },

测试

  1. git commit s

image.png
image.png