一直以来自己的 commit message 都比较随意,之前有看到同事写: feat、chore 等,同时还有相应的 emoij 表情,觉得很酷。便搜寻资料整理出此文,便于后续查询相关知识。
<type>(<scope>): <subject>
// 空一行
<body>
// 空一行
<footer>
// 示例
docs(guide): updated fixed docs from Google Docs
Couple of typos fixed:
- indentation
- batchLogbatchLog -> batchLog
- start periodic checking
- missing brace
Git 每次提交代码,都需要写 Commit message,否则不允许提交
当 Commit message 存在多行时,可以执行: git commit, 此时会进入 vim 编辑器,允许输入多行文字。
通常我们使用 git log 查看 commit 信息,如下:

我们可以使用以下命令,得到更简洁的输出:
git log <last tag> HEAD --pretty=format:%s

另外,我们可以使用 --grep 得到包含某些单词的提交,比如 --grep feature 来得到属于 feature 类型的提交:
git log <last release> HEAD --pretty=format:%s --grep feat

通过与命令行的交互,生成符合 AngularJS 规范的 commit message
具体使用参考 Commitizen 官方文档
如何在 commit 信息中添加 emoij 表情,以及不同 emoij 表情所对应的 type?
参考 git commit message emoji 使用指南
对 commit message 格式进行校验:
安装 @commitlint/cli、@commitlint/config-conventional
npm install --save-dev @commitlint/config-conventional @commitlint/cli
使用 @commitlint/config-conventional 作为 lint 配置文件: echo "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js
安装好以上依赖之后,可以在本地测试是否生效 echo "test(xxx): add test" | npx commitlint
配置 husky:
"husky": {
"hooks": {
"pre-commit": "npm run test",
"commit-msg": "commitlint -e $GIT_PARAMS" // 或者 "commitlint -E HUSKY_GIT_PARAMS"
}
},
这样就能对 commit 信息进行 lint 了。
conventional-changelog-cli 可以根据 commit 信息,通过以下命令自动生成 CHANGELOG.md 文件
npx conventional-changelog -p angular -i CHANGELOG.md -s -r 0
我们可以配置,当升级版本之后,自动生成 CHANGELOG.md
// package.json
{
scripts: {
"postversion": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0 && git add CHANGELOG.md && git commit -m\"docs(changelog): $npm_package_version\" && git push --follow-tags"
}
}
