在 github 上逛逛就能够发现,其提交的 commit 都有必定格式,工做中也有相应的规定,时间长了就能体会到其中的好处。这种约束是一种良好的实践。抽出一些时间,更详细的了解相关的资料,而后作了一些实践和总结。html
以上的好处,我的认为要有一个大的前提,就是每个提交,尽可能保证其目的单一性,好比说几个 bug 看似相似,就一次性修改提交。这样作,让 commit 的信息变的复杂化,阅读不方便,也容易让人想到一些没必要要的关联性。vue
找了几个 start 较多的库,看看提交的格式。node
网上推荐的写法是第 2 和 3 种,也就是 Angular 规范,并有配套的工具。有一个文档对 commit 的格式要求有个描述叫约定式提交。下面就根据 Angular 规范和对应文档,看看详细的说明。react
每一个 commit message 包含一个 header,body 和 footer。header 有一个特殊的格式包含有 type,scope 和 subject:git
<type>(<scope>): <subject>
<BLANK LINE>
<body>
<BLANK LINE>
<footer>
复制代码
header、body、footer 之间都要空一行,header 是必填项,scope 是选填项。commit message 的每一行的文字不能超过 100 个字符。这样子在 github 和 git 工具上更便于阅读。github
type 用于说明 commit 的类别,必须为如下类型的一种:express
scope 用于说明 commit 影响的范围,当影响的范围有多个时候,可使用 *
。npm
subject 用于对 commit 变化的简洁描述:json
body 用于对 commit 详细描述。使用祈使句,通常以动词原形开始,例如使用 change 而不是 changed 或者 changes。bash
body 应该包含此次变化的动机以及与以前行为的对比。
footer 目前用于两种状况。
1 不兼容的变更
全部不兼容的变更都必须在 footer 区域进行说明,以 BREAKING CHANGE:
开头,后面的是对变更的描述,变更的理由和迁移注释。
BREAKING CHANGE: isolate scope bindings definition has changed and
the inject option for the directive controller injection was removed.
To migrate the code follow the example below:
Before:
scope: {
myAttr: 'attribute',
myBind: 'bind',
myExpression: 'expression',
myEval: 'evaluate',
myAccessor: 'accessor'
}
After:
scope: {
myAttr: '@',
myBind: '@',
myExpression: '&',
// myEval - usually not useful, but in cases where the expression is assignable, you can use '='
myAccessor: '=' // in directive's template change myAccessor() to myAccessor } The removed `inject` wasn't generaly useful for directives so there should be no code using it.
复制代码
2 关闭 issue
若是 commit 是针对某个 issue,能够在 footer 关闭这个 issue。
## 关闭单个
Closes #234
## 关闭多个
Closes #123, #245, #992
复制代码
若是 commit 用于撤销以前的 commit,这个 commit 就应该以 revert:
开头,后面是撤销这个 commit 的 header。在 body 里面应该写 This reverts commit <hash>.
,其中的 hash 是被撤销 commit 的 SHA 标识符。
revert: feat(pencil): add 'graphiteWidth' option
This reverts commit 667ecc1654a317a13331b17617d973392f415f02.
复制代码
feat($browser): onUrlChange event (popstate/hashchange/polling)
Added new event to $browser:
- forward popstate event if available
- forward hashchange event if popstate not available
- do polling when neither popstate nor hashchange available
Breaks $browser.onHashChange, which was removed (use onUrlChange instead)
复制代码
fix($compile): couple of unit tests for IE9
Older IEs serialize html uppercased, but IE9 does not
Would be better to expect case insensitive, unfortunately jasmine does
not allow to user regexps for throw expectations.
Closes #351
复制代码
style($location): add couple of missing semi colons
复制代码
docs(guide): updated fixed docs from Google Docs
Couple of typos fixed:
- indentation
- batchLogbatchLog -> batchLog
- start periodic checking
- missing brace
复制代码
这个工具是用来给 commit 一个引导的做用,根据提示一步一步的完善 commit。
在 Windows 环境下安装,我的遇到的问题有:
尝试成功的解决方法是用 yarn 执行指令:
yarn commitizen init cz-conventional-changelog --save-dev --save-exact
复制代码
npx git-cz
无效,安装了 npx 也无效发如今 node_modules\bin\
目录下,并无 npx
相关可执行文件,须要安装 npx。
安装后仍是无效,须要用 yarn 来执行指令
yarn npx git-cz
复制代码
能够发现执行指令后,显示了对应的可执行文件的路径。
若是配置了 scripts,那么提交的时候须要执行对应的指令,才会触发这个工具的做用。
还有操做的问题,在本身的戴尔笔记本用上下键切换无效,也尝试过各类组合键,也是无效,外接的键盘有效。
原 validate-commit-msg 已不被推荐使用。安装对应要使用的提交规范,
#it also works for Windows
yarn add @commitlint/{config-conventional,cli} --dev
# Configure commitlint to use angular config
echo "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js
复制代码
示例用的是 config-conventional
规范,这个安装后,还须要使用 commitmsg
hook,推荐使用 husky。安装husky
yarn add husky --dev
复制代码
而后配置 package.json
{
"scripts": {
"commitmsg": "commitlint -E GIT_PARAMS"
}
}
复制代码
提交时候就会触发校验,效果以下图。
2018.12.09: 在最新的版本中,因为 GIT_PARAMS
参数的问题,进行了更新,配置变化以下:
{
"scripts": {
- "precommit": "npm test",
- "commitmsg": "commitlint -E GIT_PARAMS"
},
+ "husky": {
+ "hooks": {
+ "pre-commit": "npm test",
+ "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
+ }
+ }
}
复制代码
或者在项目文件夹下执行下面的命令,会自动进行更正。
./node_modules/.bin/husky-upgrade
复制代码
使用的工具是 Conventional Changelog,推荐使用 standard-version。commit 符合 Conventional Commits Specification 中描述的格式,就能够用程序自动生成 Changelog。
先进行安装。
yarn add standard-version --dev
复制代码
而后配置package.json配置执行的脚本。
{
"scripts": {
"release": "standard-version"
}
}
复制代码
执行该脚本命令,产生的结果:生成文件 CHANGELOG、自动修改库的版本号、产生一个 commit,用工具查看以下图所示。
每一次执行,不会覆盖以前的 CHANGELOG,只会在 CHANGELOG 的顶部添加新的内容。
网上有不少相似的介绍,本身动手去实践,获得的比看到的多的多。
看了北野武主演的《红鳉鱼》,里面有段台词真是精彩:
吃醋要把握好度,吃醋的人心里不急不躁,这醋也吃的回味无穷。吃醋,也就是嫉妒为什么物?本身不去努力不去付诸行动,揪着对方的弱点不放,连本身也落得下做,这就叫做嫉妒。原本为了和对手相匹敌并超越对方而努力,日复一日这个问题也会迎刃而解,但人老是作不到这一点,由于嫉妒比较轻松。
现实就是答案,就算抱怨生不逢时,社会不公,也不会有任何改变。现实就是现实,要理解现状而且分析,在那其中必定会有致使现状的缘由,对缘由有了充分认识以后再据此付诸行动就好。连现状都不懂得判断的人,在我看来就是白痴。