在 windows下使用 commitlint 和 husky 进行 git commit 约定式提交验证 / use commitlint on windows
前言
在 Windows 系统下安装和配置 commitlint 和 husky 进行 Git commit 约定式提交验证,确保提交历史的规范性。
约定式提交规范是一种基于提交信息的轻量级约定。 它提供了一组简单规则来创建清晰的提交历史; 这更有利于编写自动化工具。 通过在提交信息中描述功能、修复和破坏性变更, 使这种惯例与 SemVer 相互对应。
提交说明包含了下面的结构化元素,以向类库使用者表明其意图.
build, chore, ci, docs, feat, fix, perf, refactor, revert, style, test
更多信息见官网: https://www.conventionalcommits.org/
环境
win10
配置步骤
1. 安装 nodejs 以及 npm
Node.js 是一个基于 Chrome V8 引擎的 JavaScript 的 Runtime,用于开发服务器端应用程序。它使 JavaScript 能够运行在服务器上,而不仅仅是在浏览器中。
npm(Node Package Manager) 是 Node.js的包管理器。用来安装、管理和共享用 JavaScript 编写的代码包或模块。
- 官网下载:https://nodejs.org/
- 安装,一直 next 就好,会自动添加 PATH 和 npm
当前版本:node-v22.14.0-x64
npm 10.9.2
2. 安装 commitlint
Commitlint 是一个用于规范 Git commit 的工具。可以定义提交消息的规则,并验证每次提交时是否符合这些规则,以确保提交历史记录的清晰和一致性。这对于团队合作尤为重要,因为它可以让提交信息更具可读性和可维护性。
官方安装指南: https://commitlint.js.org/
GitHub: https://github.com/conventional-changelog/commitlint
- 在已初始化 git 的项目根目录 (同级目录中有
.git/
文件夹) 打开 git bash (不建议使用 win 下的 cmd 和 powershell) - 输入以下代码安装后,包会安装在 git 项目中的 node_modules 文件夹中
npm install --save-dev @commitlint/{cli,config-conventional}
- 创建约定式提交的规则
- 输入以下代码后,将在目录下创建一个使用 config-conventional 规则的 commitlint.config.js
echo "module.exports = { extends: ['@commitlint/config-conventional'] };" > commitlint.config.js
此时进入./node_modules/.bin/
文件夹,输入echo "foo: hello" | ./commitlint
可以测试信息验证
.../node_modules/.bin/
$ echo "foo: hello" | ./commitlint
⧗ input: foo: hello
✖ type must be one of [build, chore, ci, docs, feat, fix, perf, refactor, revert, style, test] [type-enum]
✖ found 1 problems, 0 warnings
ⓘ Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint
其他示例:
3. 安装 husky (git 钩子)
Husky 是一个 Git 钩子(hook)工具,允许在 Git 的特定操作(例如 commit、push 等)之前或之后运行自定义脚本。
commitlint 进行规则验证,要和 Git 关联需要使用 hook 工具,在 git commit 后执行验证脚本
- 同样在 git 项目中的根目录执行代码
# 安装 husky
npm install --save-dev husky
# 初始化 husky,此时会在根目录生成 .husky/ 文件夹,并生成一个 pre-commit 钩子文件
npx husky init
# commitlint 不支持 pre-commit ,创建并使用 commit-msg
echo "npx --no -- commitlint --edit \$1"> .husky/commit-msg
# 创建的 commit-msg 文件内容应该为: npx --no -- commitlint --edit $1
# 删除 pre-commit 文件,否则等着报错吧
rm .husky/pre-commit
测试 git commit 能否使用验证
touch hello
git add hello
git commit -m "foo: this will fail"
此时应该会输出
⧗ input: foo: this will fail
✖ type must be one of [build, chore, ci, docs, feat, fix, perf, refactor, revert, style, test] [type-enum]
✖ found 1 problems, 0 warnings
ⓘ Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint
husky - commit-msg script failed (code 1)
拓展: 在 github 端进行部署验证
在 github 端使用 GitHub Actions 进行部署
官方教程: https://commitlint.js.org/guides/ci-setup.html
懒得弄了
总结步骤
安装好nodejs后,在已初始化 git 的项目根目录中执行以下代码
npm install --save-dev @commitlint/{cli,config-conventional}
echo "module.exports = { extends: ['@commitlint/config-conventional'] };" > commitlint.config.js
npm install --save-dev husky
npx husky init
echo "npx --no -- commitlint --edit \$1" > .husky/commit-msg
rm .husky/pre-commit
测试 git commit 能否使用验证
touch hello
git add hello
git commit -m "foo: this will fail"
可能的问题
-
Q: npm error Missing script: "test"
A: 移除 .husky/pre-commit -
Q: ✖ Please add rules to your
commitlint.config.js
A: 检查根目录中的 commitlint.config.js,确保内容如下
module.exports = { extends: ['@commitlint/config-conventional'] };
而不是
"module.exports = { extends: ['@commitlint/config-conventional'] };"
- Q: (node:33544) [MODULE_TYPELESS_PACKAGE_JSON] Warning: Module type of file:....../commitlint.config.js is not specified and it doesn't parse as CommonJS.
A: 检查根目录中的 commitlint.config.js,确保内容如下
module.exports = { extends: ['@commitlint/config-conventional'] };
而不是
export default { extends: ['@commitlint/config-conventional'] };
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· spring官宣接入deepseek,真的太香了~