第一步 下载依赖
npm i @commitlint/config-conventional @commitlint/cli -D
第二步 配置文件
在项目的根目录下新建一个commitlint.config.js,这里边会把一个规则加上
module.exports = {extends: ['@commitlint/config-conventional']};
第三步 使用commitlint检查提交信息
错误
git commit -m "修复了一个bug"
报错
error: subject may not be empty
error: type must be one of [build, chore, ci, docs, feat, fix, perf, refactor, revert, style, test]
正确提交
git commit -m "fix: 修复了一个bug"
自定义commitlint规则
module.exports = {extends: ['@commitlint/config-conventional'],rules: {'subject-empty': [2, 'never'],'type-empty': [2, 'never'],'type-enum': [2,'always',['build','chore','ci','docs','feat','fix','perf','refactor','revert','style','test',],],'subject-case': [0],},
};
subject-empty
: 提交信息主题不能为空。type-empty
: 提交信息类型不能为空。type-enum
: 提交信息类型必须在预定义的类型列表中。subject-case
: 提交信息主题的大小写(在这个示例中,我们禁用了这个规则)。
结论
commitlint是一个强大的工具,可以帮助团队规范代码提交信息。通过使用commitlint,你可以确保提交信息清晰、一致,从而提高代码的可维护性和团队协作效率。