原文链接: Git Commit Message 应该怎么写?

最近被共事吐槽了,说我代码提交阐明写的太差。其实都不必他吐槽,我本人心里也十分分明。毕竟很多时候犯懒,都是间接一个 -m "fix" 就提交下来了。

这样做是十分不好的,我也是自食恶果,深受其害。特地是查看历史提交记录时,想通过提交阐明来理解过后的性能变更,根本不可能,都得点进去看代码才行。

所以这两天看了一些如何写好提交阐明的材料,系统地学习了一下。尽管团队没有这方面的要求,然而想要提高,得对本人提更高的要求才行。

个别应用 git 提交代码的话,能够应用 -m 参数来指定提交阐明,比方:

$ git commit -m "hello world"

如果一行不够,能够只执行 git commit,这样就会跳出文本编辑器来写多行:

$ git commit

Commit Message 格局

Commit Message 包含三个局部:Header,Body 和 Footer。

<Header><Body><Footer>

其中,Header 是必须的,Body 和 Footer 能够省略。

Header

Header 局部只有一行,包含三个字段:type(必须)、scope(可选)、subject(必须)。

<type>(<scope>): <subject>

type

type 用于阐明 commit 的类别,具体的标识如下:

  • feat:一个新的性能(feature);
  • fix:修复 bug;
  • docs:批改文档,比方 README.md、CHANGELOG.md 等;
  • style:批改代码的格局,不影响代码运行的变动,比方空格、格式化代码、补齐句末分号等等;
  • refactor:代码重构,没有新性能的增加以及 bug 修复的代码改变;
  • perf:优化代码以进步性能;
  • test:减少测试或优化改善现有的测试;
  • build:批改影响我的项目构建文件或内部依赖项,比方 npm、gulp、webpack、broccoli 等;
  • ci:批改 CI 配置文件和脚本;
  • chore:其余非 src 门路文件和测试文件的批改,比方 .gitignore、.editorconfig 等;
  • revert:代码回退;

scope

scope 用于阐明 commit 的影响范畴,比方数据层、管制层、视图层等等,视我的项目不同而不同。

如果你的批改影响了不止一个 scope,就能够应用 * 代替。

subject

subject 是 commit 目标的简略形容,不超过 50 个字符,结尾不须要句号。

Body

Body 局部是对本次 commit 的详细描述,能够分多行。

Body 局部应该阐明代码变动的动机,以及与以前行为的比照。

More detailed explanatory text, if necessary.  Wrap it toabout 72 characters or so.Further paragraphs come after blank lines.- Bullet points are okay, too- Use a hanging indent

Footer

Footer 局部次要用于两种状况:不兼容变动和解决 Issue。

不兼容变动

如果以后代码与上一个版本不兼容,则 Footer 局部以 BREAKING CHANGE: 结尾,前面就是对变动的形容、以及变动理由和迁徙办法。

BREAKING CHANGE: Previously, $compileProvider.preAssignBindingsEnabled was set to true by default. This means bindings were pre-assigned in component constructors. In Angular 1.5+ the place to put the initialization logic relying on bindings being present is the controller $onInit method.To migrate follow the example below:Before:```jsangular.module('myApp', [])    .component('myComponent', {        bindings: {value: '<'},        controller: function() {        this.doubleValue = this.value * 2;    }});```After:```jsangular.module('myApp', [])    .component('myComponent', {        bindings: {value: '<'},        controller: function() {            this.$onInit = function() {                this.doubleValue = this.value * 2;            };        };        this.doubleValue = this.value * 2;    }});```Don't do this if you're writing a library, though, as you shouldn't change global configuration then.

解决 Issue

解决 Issue 分为两种状况,别离是关联 Issue 和敞开 Issue。

比方本次提交如果和某个 issue 有关系:

Issue #1, #2, #3

如果以后提交信息解决了某个 issue:

Close #1, #2, #3

Revert

还有一种非凡状况,如果以后 commit 用于撤销以前的 commit,则必须以 revert: 结尾,前面跟着被撤销 commit 的 Header。

revert: feat(pencil): add 'graphiteWidth' optionThis reverts commit 667ecc1654a317a13331b17617d973392f415f02.

Body 局部的格局是固定的,必须写成 This reverts commit &lt;hash>.,其中 hash 是被撤销 commit 的 SHA 标识符。

如果以后 commit 与被撤销的 commit,在同一个公布(release)外面,那么它们都不会呈现在 Change log 外面。如果两者在不同的公布,那么以后 commit,会呈现在 Change log 的 Reverts 小标题上面。

最初来看一个例子,算是一个总结,至于具体内容还是要依据理论状况来填写。

feat: 增加了分享性能给每篇博文增加了分享性能- 增加分享到微博性能- 增加分享到微信性能- 增加分享到朋友圈性能Issue #1, #2Close #1

插件举荐

有了这些标准,也晓得怎么写了,然而不是会放心记不住呢?不要怕,有插件能够用,如果应用 VsCode 的话,能够装置一个叫 Commit Message Editor 的插件。

能够依据提示信息间接写:

也能够应用表单的形式,有选项能够抉择:

这样不仅能够很不便地写提交阐明了,还能够使提交阐明更加的标准。

以上就是本文的全部内容,如果感觉还不错的话欢送点赞转发关注,感激反对。


参考文章:

  • https://juejin.cn/post/6960541430473293837
  • https://mrseawave.github.io/blogs/articles/2021/03/31/git-com...

举荐浏览:

  • Git 分支管理策略