# git commit 规范

# commit 格式: <type>(<scope>): <subject>

type(必须):

  • feat: 新功能、新特性
  • fix: 修改 bug
  • perf: 更改代码,性能优化
  • refactor: 代码重构(重构,在不影响代码内部行为、功能下的代码修改)
  • docs: 文档修改
  • style: 代码格式修改,注意不是 css 修改(例如分号修改)
  • test: 测试用例新增、修改
  • build: 影响项目构建或依赖项修改
  • revert: 恢复上一次提交
  • ci: 持续集成相关文件修改
  • chore: 构建过程或辅助工具的变动
  • release: 发布新版本
  • workflow: 工作流相关文件修改
    scope(可选): commit 影响的范围
    subject(必须): subject 是 commit 目的的简短描述,不超过 50 个字符

# 通过 git pre-commit/commit-msg 钩子函数进行自动化提交验证

pre-commit 可以用于在 commit 之前检查代码格式
commit-msg 可以用于检查 commit message 的格式

pre-commit 可以配置 eslint 使用

这里主要介绍 commit-msg

# commitlint.config.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// @ts-check

/** @type {import('@commitlint/types').UserConfig} */
const config = {
extends: ['@commitlint/config-conventional'],
rules: {
'type-enum': [
2,
'always',
[
'feat',
'fix',
'docs',
'style',
'refactor',
'perf',
'test',
'chore',
'revert',
'build',
'ci',
'workflow',
],
],
'type-case': [2, 'always', 'lower-case'],
'type-empty': [2, 'never'],
'scope-empty': [0],
'scope-case': [2, 'always', 'lower-case'],
'subject-empty': [2, 'never'],
'subject-full-stop': [2, 'never', '.'],
'subject-case': [2, 'never', ['sentence-case', 'start-case', 'pascal-case', 'upper-case']],
'header-max-length': [2, 'always', 72],
},
};

module.exports = config;

# .husky/commit-msg

1
npx --no -- commitlint --edit ${1} 

# 使配置生效

1
npx husky