Git 每次提交代码,都要写 Commit message 来讲明本次的提交内容。vue
git commit -m "hello word"
复制代码
当提交的内容比较多的时候, 能够执行 git commit
, 使用跳出来的文原本编辑提交信息。git
git commit
复制代码
在项目开发过程当中, 使用 git commit 提交的信息能够很好的反应项目的开发进展状况。 因此应当规范 git message 的格式, 来更加清晰明了的说明每次的提交目的。github
关于 Git message 的写法规范社区中有不少种, 目前使用较为普遍的是 Angular 规范npm
在 Angular
规范中, 每次提交, Commit message 都包括三个部分: Header、Body 和 Footer;编程
<type>(<scope>):<subject> ## header 部分
// 空一行
<body> ## body 部分
// 空一行
<footer> ## footer 部分
复制代码
::: tipjson
Header 部分只有一行, 包括三个字段: type
(必须)、scope
(可选)、subject
(必须);bash
type
用于说明 commit 的类型, 在 Angular 规范
种, 只容许 7 种经常使用类型和一种特殊类型(revert):工具
feat: 新功能 (feature)
fix: 修补 bug
docs: 文档 (ocumentation)
style: 格式 (不影响代码运行)
refactory: 重构 (既不是新增功能, 也不是修改 bug 的代码改动)
test: 增长测试
chore: 构建过程或辅助工具的变更
revert: 撤销之前的 commit; header 部分须要跟被撤销 Commit 的 Header
复制代码
scope
用于说明 commit
影响的范围, 好比说数据层、控制层、视图层等等测试
subject
是 commit
的简短描述, 不超过 50 个字符。ui
::: tip 以动词开头, 使用第一人称如今时, 好比 change
而不是 changed
或者 changes
第一个字母小写 结尾不加句号 :::
body
部分是对本次 commit
的详细描述, 能够分为多行。
在不少状况下, 是不使用这部分的内容的, 可是在下面两种状况下,回使用到 footer
footer
部分须要使用 BREAKING CHANGE
开头, 后面是对变更的描述以及变更理由和变更方法.BREAKING CHANGE: isolate scope bindings definition has changed.
To migrate the code follow the example below:
Before:
scope: {
myAttr: 'attribute',
}
After:
scope: {
myAttr: '@',
}
The removed `inject` wasn't generaly useful for directives so there should be no code using it. 复制代码
issue
, 那个能够在 footer 部分关闭 issue
Closes #123
复制代码
关闭多个 issue
Closes #123, #234, #345
复制代码
在项目的开发过程当中,编程人员遵照 Angular 规范
须要编写更多的 git message 信息, 使用 Commitizen 来进行交互式的 message 输入。
npm install -g commitizen
复制代码
而后,在项目目录里,运行下面的命令,使其支持 Angular 的 Commit message 格式。
commitizen init cz-conventional-changelog --save --save-exact
复制代码
当须要 git commit
的时候使用 git cz
来生成符合规范的 git message.
git cz
命令为 git message 提供了一个规范的 message 模板, 这时使用 git commit -m
或者 使用 git cz
依旧不能严格限制 git message 内容, 为了严格规范须要使用 commitlint 来拒毫不规范的 git message 内容。
npm install --save-dev @commitlint/config-conventional @commitlint/cli
复制代码
echo "module.exports = {extends: ['@commitlint/config-conventional']};" > commitlint.config.js
复制代码
package.json
文件中添加"commitlint": {
extnds: [
"@commitlint/config-conventional"
]
}
复制代码
配置好 commitlint
, 还须要设置 触发 commitlint
的时机。
Git
中自带不一样的 hook
, 当某些事件发生的时候,会触发相对应的 hook
, 这些 hook
脚本存放在项目根目录的 .git/hooks
目录下。
commit-msg
是其中一个 hook
, 在 git commit
提交的时候触发。 可使用 husky 来自定义 commit-msg
触发时候的事件。
安装 npm install husky -D
在 package.json
中添加代码
"config": {
"ghooks": {
"commit-msg": "commitlint -e $GIT_PARAMS"
}
}
复制代码
在 git commit-msg
这个钩子中会触发 commitlint
的操做。
在完成了 git message
的校验以后, 能够继续使用 lint-staged 和 git hooks
来进行代码提交前的语法、风格的验证和修复。
安装 npm install -D lint-staged
在跟目录下建立 .lintstagedrc
, 并写入
{
"*.{js,vue}": ["eslint --fix", "git add"]
}
复制代码
package.json
中写入"lint-staged": {
"*.{js,vue}": ["eslint --fix", "git add"]
}
复制代码
husky
配置中添加触发时机"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
}
复制代码
这样在每次提交以前都会触发 pre-commit
这个 hook
, 从而触发 .lintstagedrc
或者 package.json
中的 lint-staged
里面的配置。 在例子中,咱们配置了对全部 .js
或者 .vue
结尾的文件进行 eslint
的修复, 而且当修复以后再次执行 git add
将修改后的文件再次放到暂存区。 这样就能够保证每次提交的代码都是统一风格的代码了。
::: tip lint-staged
只对这次提交所在暂存区的文件(git add后的文件)进行一系列的检查、修复、格式化操等做。 :::
当使用 Angular 规范
提交 git message
, 还可使用 standard-version生成 Change log
文档。 生成的文档将会包括下面三个部分。
new features // 新增功能记录
bug fixes // 解决 bug 记录
breaking changes // 不兼容变更记录
复制代码
每一部分都会列出相关的 commit
, 而且指向这些 commit
的链接。 conventional-changelog
使用以下:
npm install -g standard-version
复制代码
package.json
中添加 script
字段"script": {
"release": "standard-version"
}
复制代码
运行 npm run release
将会执行下面的步骤。
1. 修改 package.json package-lock.json 中的版本号
2. 生成 CHANGELOG.md 文件。
3. 提交 package.json package-lock.json CHANGELOG.md 文件
4. 给本次提交打上 tag
复制代码