1、下载husky(使用8.0.1版本)
1)npm install husky@8.0.1 --save-dev

2)初始化目录.husky
npx husky install

3)新增commit-msg目录并写入信息
npx husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"'
.husky文件夹是新增在根目录下


2、下载commitlint
commitlint 是检测我们提交的规范的,具体规范如下(冒号匹配的是英文冒号,并且冒号后要有空格):
<type>: <subject>
常见 type 类型 :
· upd:更新某功能(不是 feat, 不是 fix)
指令 | 意思 |
---|---|
feat | 新功能(feature) |
fix | 修复 bug |
docs | 文档(documentation) |
style | 代码格式调整(不影响代码运行的变动,如空格、缩进) |
refactor | 代码重构(即不是新增功能,也不是修改 bug 的代码变动,不涉及功能变更) |
test | 测试用例修改或新增 |
chore | 构建过程或辅助工具的变动(如.gitignore调整) |
revert | 撤销某次提交 |
1)下载
npm install @commitlint/cli @commitlint/config-conventional --save-dev
2)在项目根目录新建配置文件commitlint.config.js


代码如下:
js
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],
},
};
3、验证
1)命令行验证:
先git add 想要提交的代码
再git commit -m "bad message",会报错误信息

需要加入规范前缀,提交后再push

2)vsCode验证
不符合规范提交报错:

符合格式正确提交:

