.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}
CommitMsg
git 提交规范
背景
团队协作开发,git 作为一个开源的分布式版本控制系统,俨然成为当下最受欢的项目代码版本管理工具,
即是团队,就要有一定的规矩,规范,这样才能更好的发挥团队效率。熟悉 git 的小伙伴都知道,每次提交代码,
都要写 Commit message(提交说明),否则就不允许提交,至于提交的信息 git 并没有进行约束,以至于,
提交上来的说明是五花八门,由此社区出现了多种写法规范,其中
Angular 规范 是目前最为广泛的写法,比较合理和系统化。
本文就来讲一讲两种使用 Angular 规范的方法。
法一,基于项目通过 hooks 触发(我司团队用的此方法)
这种方法的好处是,任何人只要想往本项目提交 commit,就必须按照规范,否则不让提交。
- 安装所需依赖,
npm i --save-dev husky chalk; - 在项目的根目录新建一个文件夹
.github(名字随意),这个文件夹专门用来存放 github 相关的东西; - 在上一步的文件夹中新建
node脚本文件verifyCommitMsg(名字随意); - 在上一步的脚本文件中写入以下代码:
#!/bin/env node
const chalk = require('chalk');
const msgPath = process.env.HUSKY_GIT_PARAMS;
if (!msgPath) {
console.error(chalk.red(`process.env.HUSKY_GIT_PARAMS can't be ${msgPath},please check`));
process.exit(1);
}
console.log('HUSKY_GIT_PARAMS: ', msgPath);
const msg = require('fs')
.readFileSync(msgPath, 'utf-8')
.trim();
console.log(msg);
const commitRE = /^(revert: )?(feat|fix|docs|style|refactor|perf|test|workflow|ci|chore|types)(\(.+\))?: .{1,50}/;
if (!commitRE.test(msg) && !/merge/ig.test(msg)) {
console.log();
console.error(
` ${chalk.bgRed.white(' ERROR ')} ${chalk.red(`invalid commit message format.`)}\n\n` +
chalk.red(
` Proper commit message format is required for automated changelog generation. Examples:\n\n`
) +
` ${chalk.green(`feat(compiler): add 'comments' option`)}\n` +
` ${chalk.green(`fix(v-model): handle events on blur (close #28)`)}\n\n` +
chalk.red(` See .github/COMMIT_CONVENTION.md for more details.\n`)
// chalk.red(` You can also use ${chalk.cyan(`npm run commit`)} to interactively generate a commit message.\n`)
);
process.exit(1);
}
- 在
package.json增加/修改 hooks,如下:
...
"husky": {
"hooks": {
"commit-msg": "node .github/verifyCommitMsg"
}
},
...
这样就做到了每次提交都会检查 commit 信息了,当不符合规范时,会给出提示。
例如:
法二,基于 npm 依赖插件
这种方法的好处是,提交比较人性化。
- 全局安装依赖,
npm i -g commitizen - 初始化适配器,
commitizen init cz-conventional-changelog --save-dev --save-exact - 用
git cz命令 代替git commit
例如:
===🧐🧐 文中不足,欢迎指正 🤪🤪===