Commitlint 是一个用于检查 Git 提交的 message 是否符合项目的规范的工具。规范的提交内容可以让开发者方便快速浏览查找比较代码,可以直接从 提交内容 生成 Change log,触发 CI 流程。
| 1 | echo '提交内容' | npx commitlint | 
命令行工具
安装
全局安装 @commitlint/cli
| 1 | npm install -g @commitlint/cli | 
配置规则
@commitlint/config-conventional 包含了 约定式提交 的规则。
安装
| 1 | npm install -g @commitlint/config-conventional | 
创建 commitlint.config.js 文件来配置
| 1 | // commitlint.config.js | 
使用 commitlint 命令来测试 commit message
运行 echo '提交内容' | commitlint 来测试 commit message。commitlint 命令会打印规范检查信息和返回状态码。
| 1 | Lint from stdin | 
npx 使用
| 1 | npx @commitlint/cli | 
配合 Husky 使用
我写了一篇 Husky 入门教程 博文。下面介绍 Commitlint 和 Husky 配合,在提交 message 前自动使用 Commitlint 检查提交规范。
按照 Husky 入门教程 配置好 Husky, 使用下面的命令添加 Husky 的 commit-msg 的 hook:
| 1 | npx husky add .husky/commit-msg 'npx --no -- commitlint --edit $1' | 
配制好后,提交时,Commitlint 会自动检查 commit message 是否符合项目中 commitlint.config.js 文件配制的规范。
 
        