欢迎使用CSDN-markdown编辑器

来源:互联网 发布:新手淘宝客哪里交流 编辑:程序博客网 时间:2024/06/01 20:25

Git commit 规范

规范Git commit的好处

在说明规范commit message的好处之前对比一下规范的commit message和无规范的commit message:

没规范的commit message
没规范的commit message
规范的commit message
规范的commit message

1.前者的commit message长度不一,格式多样,后者精简格式统一.

2.工程repo的开发者可以通过良好的设计的commit message来看出工程开发的更新变动,而且沟通起来也方便

3.好的commit message对项目的开发和维护是一个良性循环.一个格式正确,持续的的commit message不需要花费过多的经历去追踪和使用commit log,如果不会使用commit log有时不利于问题的跟踪和维护

4.一个好的commit格式有助于git log,blame,revert,rebase,log,shortlog等Git 命令的使用.也有利于代码审核.过了很久之后仍能看到commit message就知道之前干了什么

iOS小组commit格式-如何写好Git commit

commit message 结构

集合网上的一些资源可以总结一个commit message的结构应该包括一下三部分:
1. subject //主题
2. body //详细描述可以放这里
3. footer //附录,备注

great commit message 的特点

The seven rules of a great Git commit message
1 Separate subject from body with a blank line
2 Limit the subject line to 50 characters
3 Capitalize the subject line
For example:
==• Accelerate to 88 miles per hour==
Instead of:
• accelerate to 88 miles per hour
4 Do not end the subject line with a period
Example:
==• Open the pod bay doors==
Instead of:
• Open the pod bay doors.
5 Use the imperative mood in the subject line
A few examples:
==• Refactor subsystem X for readability==
==• Update getting started documentation==
==• Remove deprecated methods==
==• Release version 1.0.0==
Instead of:
• Fixed bug with Y
• Changing behavior of X
6 Wrap the body at 72 characters
7 Use the body to explain what and why vs. how

一个好的commit message 格式(范例)–Angular 规范

Angular 规范格式如下:

<type>(<scope>): <subject><BLANK LINE><body><BLANK LINE><footer>

其中,header 是必需的,body 和 footer 可以省略。
不管是哪一个部分,任何一行都不得超过72个字符(或100个字符)。这是为了避免自动换行影响美观。

Header
Header部分只有一行,包括三个字段:type(必需)、scope(可选)和subject(必需)。
type
用于说明 commit 的类别,只允许使用下面7个标识。

•   feat:新功能(feature)•   fix:修补bug•   docs:文档(documentation)•   style: 格式(不影响代码运行的变动)•   refactor:重构(即不是新增功能,也不是修改bug的代码变动)•   test:增加测试•   chore:构建过程或辅助工具的变动

如果type为feat和fix,则该 commit 将肯定出现在 Change log 之中。其他情况(docs、chore、style、refactor、test)由你决定,要不要放入 Change log,建议是不要。

scope
scope用于说明 commit 影响的范围,比如数据层、控制层、视图层等等,视项目不同而不同。
例如在Angular,可以是location,browser, compile,rootScope, ngHref, ngClick, ngView等。
如果你的修改影响了不止一个scope,你可以使用*代替。
subject
subject是 commit 目的的简短描述,不超过50个字符。
其他注意事项:
• 以动词开头,使用第一人称现在时,比如change,而不是changed或changes
• 第一个字母小写
• 结尾不加句号(.)

Body
Body 部分是对本次 commit 的详细描述,可以分成多行。

有两个注意点:
• 使用第一人称现在时,比如使用change而不是changed或changes。
• 永远别忘了第2行是空行
• 应该说明代码变动的动机,以及与以前行为的对比。

Footer
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
如果当前 commit 针对某个issue,那么可以在 Footer 部分关闭这个 issue 。

Closes #234

Git commit 工具-Commitizen

Commitizen是用来给commit固定提交格式的

安装 请参考:https://github.com/commitizen/cz-cli

npm install -g commitizen

安装commitizen适配器(Adapter),例如:cz-conventional-changelog

npm install -g cz-conventional-changelog

配置适配器的path:

echo '{ "path": "cz-conventional-changelog" }' > ~/.czrc

安装成功后,凡是用到git commit命令,一律改为使用git cz。这时,就会出现选项,用来生成符合格式的 Commit message。命令提交如下:

zxx's iMac:callwatch crazyman$ git cz -acz-cli@2.9.6, cz-conventional-changelog@1.2.0Line 1 will be cropped at 100 characters. All other lines will be wrapped after 100 characters.? Select the type of change that you're committing: style:    Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)? Denote the scope of this change ($location, $browser, $compile, etc.): 搜索框? Write a short, imperative tense description of the change: 弹出XTCSearchController时改变其背景? Provide a longer description of the change: 弹出XTCSearchController时改变其背景,使其展现时更友好? List any breaking changes or issues closed by this change: @yuanhongyang[trunk 7ce592548] style(搜索框): 弹出XTCSearchController时改变其背景 1 file changed, 5 insertions(+)

自定义一个Commitizen工具了

1.自定义type
例如:

  • merge 合并分支,(能不能让主干定期自动合到下拉分支上)
  • update 更新,同一个点重复修改

2.自定义scope

3.在subject中加上reviewer
例如: fix(SMSCollectionViewController):iphone 5s屏幕适配bug-review:@yuanhongyang

4.在commit message中add reviewer通知reviewer来审核盖茨提交的代码

可以参考:
1.https://github.com/leonardoanalista/cz-customizable
2.https://www.npmjs.com/package/cz-jira-smart-commit

参考文档:

  • https://chris.beams.io/posts/git-commit/

  • http://www.ruanyifeng.com/blog/2016/01/commit_message_change_log.html

  • https://segmentfault.com/a/1190000009048911
  • https://github.com/commitizen/cz-cli
原创粉丝点击