程序员最烦的几件事:写测试,变量命名,还有填代码提交信息(commit message)。翻几个开源项目遍立刻能够回味那做文凑字数的青春时光。javascript
其实 commit message 的做用远不止如此,通过简单的配置即可无痛成为代码提交的文明公民。java
最起码的一点,项目的提交历史是其余人(包括将来的本身)了解项目的一个重要途径。好的提交历史能够方便其余人参与进来,也能够方便本身快速定位问题。git
此外,提交信息还能够用来触发 CI 构建,自动生成 CHANGELOG ,版本自动语义化提高…… 只须要一点点配置就能够干这么多,真是懒人必备。程序员
跟 Code Style 同样,Commit Message 也有各类风格。若是没什么特殊癖好推荐用基于 Angular ,
后独立开来的 Conventional Commits 风格。
它也基本是各个工具的默认配置,因此搭配起来不须要折腾。github
虽然规则很少,但不必定能随时记住,特别是对新人,必需要有友好的方式提交。npm
commitizen 是一个很好的选择,经过命令行回答几个问题便可填完信息,减轻了记忆负担。
它是一个通用的工具,经过插件方式支持各类风格。咱们选择 Conventional 须要安装
cz-conventional-changelog 。json
npm install --save-dev commitizen cz-conventional-changelog
而后配置 package.json
就能够经过 npm run commit
提交。svg
{ "scripts": { "commit": "git-cz" } }
另外 VSCode 用户也能够用 vscode-commitizen ,
经过 ctrl+shift+p
或 command+shift+p
提交。工具
没错,Commit Message 也有 Linter ,可对 Commit Message 进行检验,杜绝打字手残和浑水摸鱼。测试
这里用 commitlint 配合 husky 实现自动检测。
commitlint 也是通用的工具,须要同时安装风格配置。 husky 能够方便使用 git hooks ,在 commit 时触发 commitlint 。
npm install --save-dev @commitlint/cli @commitlint/config-conventional husky
项目根新建 commitlint.config.js
module.exports = { extends: ['@commitlint/config-conventional'] }
配置 package.json
{ "husky": { "hooks": { "commit-msg": "commitlint -E HUSKY_GIT_PARAMS" } }, }
最后安装 standard-version 实现自动生成 CHANGELOG 和版本自动语义化提高。
npm install --save-dev standard-version
配置 package.json
{ "scripts": { "release": "standard-version" } }
第一次发布时能够用如下命令重置
npm run release -- --first-release
之后直接 npm run release
便可。
也能够手动指定版本:
# npm run script npm run release -- --release-as minor # Or npm run release -- --release-as 1.1.0
在 README 中加入小徽章可方便其余人了解风格。
[](http://commitizen.github.io/cz-cli/) [](https://conventionalcommits.org)
安装
npm install --save-dev commitizen cz-conventional-changelog @commitlint/cli @commitlint/config-conventional husky standard-version
配置 package.json
{ "scripts": { "commit": "git-cz", "release": "standard-version" }, "husky": { "hooks": { "commit-msg": "commitlint -E HUSKY_GIT_PARAMS" } }, }
项目根新建 commitlint.config.js
module.exports = { extends: ['@commitlint/config-conventional'] }
【完】