Skip to content

使用 commitlint 规范 commit 信息

安装 commitlint

安装 commitlint 和 commitlint config-conventional,然后 package.json 中配置 commitlint 以使用它。

json
"commitlint": {
  "extends": ["@commitlint/config-conventional"]
}

commitlint 使用 cosmiconfig 来支持配置文件,这意味着您还可以通过 .commitlintrc、commitlint.config.js 等文件配置 commitlint。

安装 husky

现在不按照 @commitlint/config-conventional 规范提交的 commit 会被阻止并给出错误提示:

bash
$ git commit -m 'project init'
   input: project init
   subject may not be empty [subject-empty]
   type may not be empty [type-empty]

   found 2 problems, 0 warnings
   Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint

husky - commit-msg hook exited with code 1 (error)