Git Commit强制规范(commitlint+husky)

从我的notion文档迁移到的博客园:[https://www.notion.so/Git-Commit-commitlint-husky-9cc381b35484437ca32f474cda40dc24)

Git Commit强制规范(commitlint+husky)

一、缘起

  • 规范前

Git Commit强制规范(commitlint+husky)

  • 规范后

Git Commit强制规范(commitlint+husky)

二、工具介绍

1、commitlint

Git Commit强制规范(commitlint+husky)

Git Commit强制规范(commitlint+husky)

commitlint 是当前使用最为广泛的 git commit 校验约束工具之一,

commitlint helps your team adhering to a commit convention. By supporting npm-installed configurations it makes sharing of commit conventions easy.

(1)安装

npm install -g @commitlint/cli @commitlint/config-conventional

config-conventional是社区整理的常用的Commit规范,见如下

https://www.npmjs.com/package/@commitlint/config-conventional

(2)配置

生成commitlint.config.js配置文件,命令如下:

echo "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js
  • 默认采用config-conventional:

    module.exports = {extends: ['@commitlint/config-conventional']};
    
  • 可自定义配置:

    配置规则见 https://commitlint.js.org/#/reference-configuration

    module.exports = {
        extends: ['@commitlint/config-conventional'],
        rules: {
            'type-enum': [2, 'always', [
                "feat", "upd", "del", "fix", "refactor", "test", "perf", "docs", "style", "revert"
            ]],
            'subject-full-stop': [0, 'never'],
            'subject-case': [0, 'never']
    
        }
    };
    

2、husky

Husky can prevent bad git commit, git push and more

上一篇:代码风格工具整理


下一篇:用commitlint规范团队的git提交信息