Java学习资料-Git 提交规范

规范是建立在程序开发者与程序阅读者一个沟通的桥梁,是一个团队必须要严格遵守的约定

——动力节点Java学院

一、为什么需要规范?

无规矩不成方圆,编程也一样。

如果你有一个项目,从始至终都是自己写,那么你想怎么写都可以,没有人可以干预你。可是如果在团队协作中,大家都张扬个性,那么代码将会是一团糟,好好的项目就被糟践了。不管是开发还是日后维护,都将是灾难。

这时候,有人提出了何不统一标准,大家都按照这个标准来。于是ESLint,JSHint 等代码工具如雨后春笋般涌现,成为了项目构建的必备良品。

Git Commit 规范可能并没有那么夸张,但如果你在版本回退的时候看到一大段糟心的 Commit,恐怕会懊恼不已吧。所以,严格遵守规范,利人利己。

二、具体规则

先来看看公式:

[if !supportLists]· [endif]type

用于说明commit的类别,只允许使用下面7个标识。

[if !supportLists]· [endif]scope

用于说明commit影响的范围,比如数据层、控制层、视图层等等,视项目不同而不同。

[if !supportLists]· [endif]subject

是commit目的的简短描述,不超过50个字符。

1.以动词开头,使用第一人称现在时,比如change,而不是changed或changes

2.第一个字母小写

3.结尾不加句号(.)

规范参考自阮一峰老师的文章:Commit message和 Change log 编写指南。


三、异常处理

我们先来看看这个异常提醒:

[if !supportLists]1. [endif]INVALID COMMIT MSG: does not match "(): " !

[if !supportLists]2. [endif]

[if !supportLists]3. [endif]jartto:fix bug


这里之所以报出这个警告,是因为我的提交出现了两个问题:

其一,使用了规范外的关键字;其二,很细节的问题,jartto:后少了空格;

这时候我才回忆起来,当时提交一直失败,情急之下直接强制提交,所以以后的提交都会抱出这个异常。大致意思就是:

你的之前的Commit 不合格~你的之前的 Commit 不合格~你的之前的 Commit 不合格

这时候就很烦了,我们只能去将之前的错误修正,那么如何操作呢?

四、如何修改之前的commit 信息?

其实并不复杂,我们只需要这样做:

1、将当前分支无关的工作状态进行暂存

[if !supportLists]1. [endif]git stash


2、将 HEAD 移动到需要修改的 commit 上

[if !supportLists]1. [endif]git rebase 9633cf0919^ --interactive

3、找到需要修改的 commit ,将首行的 pick 改成 edit 4、开始着手解决你的 bug 5、 git add 将改动文件添加到暂存 6、 git commit –amend 追加改动到提交 7、git rebase –continue 移动 HEAD 回最新的 commit 8、恢复之前的工作状态

[if !supportLists]1. [endif]git stash pop

大功告成,是不是想把整个Commit 都修改一遍,逃~

此处参考自:修改Commit 日志和内容

五、项目中使用

这时候问题又来了,为什么我提交的时候会有警告,这个又是如何做到的呢?

这时候,我们需要一款Node 插件 validate-commit-msg 来检查项目中 Commit message 是否规范。

1.首先,安装插件:

[if !supportLists]1. [endif]npm install --save-dev validate-commit-msg


2.使用方式一,建立 .vcmrc 文件:

[if !supportLists]1. [endif]{

[if !supportLists]2. [endif]"types": ["feat", "fix", "docs", "style", "refactor", "perf", "test", "build", "ci", "chore", "revert"],

[if !supportLists]3. [endif]

[if !supportLists]4. [endif]"scope": {

[if !supportLists]5. [endif]

[if !supportLists]6. [endif]"required": false,

[if !supportLists]7. [endif]

[if !supportLists]8. [endif]"allowed": ["*"],

[if !supportLists]9. [endif]

[if !supportLists]10. [endif]"validate": false,

[if !supportLists]11. [endif]

[if !supportLists]12. [endif]"multiple": false

[if !supportLists]13. [endif]

[if !supportLists]14. [endif]},

[if !supportLists]15. [endif]

[if !supportLists]16. [endif]"warnOnFail": false,

[if !supportLists]17. [endif]

[if !supportLists]18. [endif]"maxSubjectLength": 100,

[if !supportLists]19. [endif]

[if !supportLists]20. [endif]"subjectPattern": ".+",

[if !supportLists]21. [endif]

[if !supportLists]22. [endif]"subjectPatternErrorMsg": "subject does not match subject pattern!",

[if !supportLists]23. [endif]

[if !supportLists]24. [endif]"helpMessage": "",

[if !supportLists]25. [endif]

[if !supportLists]26. [endif]"autoFix": false

[if !supportLists]27. [endif]

[if !supportLists]28. [endif]}

3.使用方式二:写入 package.json

[if !supportLists]1. [endif]

{


[if !supportLists]2. [endif]

[if !supportLists]3. [endif]"config": {

[if !supportLists]4. [endif]

[if !supportLists]5. [endif]"validate-commit-msg": {

[if !supportLists]6. [endif]

[if !supportLists]7. [endif]/* your config here */

[if !supportLists]8. [endif]

[if !supportLists]9. [endif]}}

[if !supportLists]10. [endif]}

4.可是我们如果想自动使用 ghooks 钩子函数呢?

[if !supportLists]1. [endif]{

[if !supportLists]2. [endif]…

[if !supportLists]3. [endif]

[if !supportLists]4. [endif]"config": {

[if !supportLists]5. [endif]

[if !supportLists]6. [endif]"ghooks": {

[if !supportLists]7. [endif]

[if !supportLists]8. [endif]"pre-commit": "gulp lint",

[if !supportLists]9. [endif]

[if !supportLists]10. [endif]"commit-msg": "validate-commit-msg",

[if !supportLists]11. [endif]

[if !supportLists]12. [endif]"pre-push": "make test",

[if !supportLists]13. [endif]

[if !supportLists]14. [endif]"post-merge": "npm install",

[if !supportLists]15. [endif]

[if !supportLists]16. [endif]"post-rewrite": "npm install",

[if !supportLists]17. [endif]

[if !supportLists]18. [endif]…

[if !supportLists]19. [endif]

[if !supportLists]20. [endif]}

[if !supportLists]21. [endif]

[if !supportLists]22. [endif]}

[if !supportLists]23. [endif]

[if !supportLists]24. [endif]…

[if !supportLists]25. [endif]

[if !supportLists]26. [endif]}

六、Commit 规范的作用

1.提供更多的信息,方便排查与回退;2.过滤关键字,迅速定位;3.方便生成文档;

七、生成Change log

正如上文提到的生成文档,如果我们的提交都按照规范的话,那就很简单了。生成的文档包括以下三个部分:

[if !supportLists]· [endif]New features

[if !supportLists]· [endif]Bug fixes

[if !supportLists]· [endif]Breaking changes.

每个部分都会罗列相关的commit ,并且有指向这些 commit 的链接。当然,生成的文档允许手动修改,所以发布前,你还可以添加其他内容。

这里需要使用工具Conventional Changelog 生成 Change log :

[if !supportLists]1. [endif]npm install -g conventional-changelog

[if !supportLists]2. [endif]

[if !supportLists]3. [endif]cd jartto-domo

[if !supportLists]4. [endif]

[if !supportLists]5. [endif]conventional-changelog -p angular -i CHANGELOG.md -w


为了方便使用,可以将其写入package.json的 scripts 字段。

[if !supportLists]1. [endif]{

[if !supportLists]2. [endif]

[if !supportLists]3. [endif]"scripts": {

[if !supportLists]4. [endif]

[if !supportLists]5. [endif]"changelog": "conventional-changelog -p angular -i CHANGELOG.md -w -r 0"

[if !supportLists]6. [endif]

[if !supportLists]7. [endif]}

[if !supportLists]8. [endif]

[if !supportLists]9. [endif]}

这样,使用起来就很简单了:

[if !supportLists]1. [endif]npm run changelog

到这里,我们所有的问题都搞明白了,�Cheers~

八、总结

看完文章,你还会如此放荡不羁吗?你还会随心所欲的编写Commit 吗?你还会如此 git commit -m "hello jartto"提交吗?

答案是否定的,因为使用了钩子函数,你没有机会了,否则将是无穷无尽的恢复Commit。这倒可以养成良好的提交习惯,�~


动力节点Java架构师班深度剖析Java底层原理,热门技术深入探讨,前沿技术深入解读,大项目实战重构,从0到1做架构,从全局思维出发,带你把控大型项目中别人忽略的重要细节节点,站在巨人肩膀上学习架构师,带你领会架构师不一样的视野

你可能感兴趣的:(Java学习资料-Git 提交规范)