美文网首页
Git提交消息

Git提交消息

作者: HelenYin | 来源:发表于2017-10-30 22:48 被阅读0次
    如何给git commit 添加一段简短且有意义的描述

    每一个commit应该包括以下几部分:header,body,footer
    header包括type,scope,subject

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

    type

    type是以下的几个:

    • feat: 新功能
    • fix: 修复bug
    • docs: 只有文档改变
    • style: 并没有影响代码的意义(空格,去掉分号,格式的修改等)
    • refactor: 代码的修改并没有修改bug,也没有添加新功能
    • perf: 代码的修改提高的性能
    • test: 添加测试
    • chore: 构建过程或构建工具的改变(并没有生产环境代码的改变)

    Scope

    scope是可选的,用来说明本次提交影响的范围(在angular的代码提交中会写这些$location, $browser, $compile, $rootScope, ngHref, ngClick, ngView

    Subject

    Subject最多50个字

    • 使用命令式
    • 首字母大写
    • 不要用“.”结尾

    Body


    并不是所有的commit都需要body,只有当这个commit需要一些说明的时候才添加body。有时候一行Subject来说明足以。body中备注代码改动的什么,以及为什么要这样改(而不是怎么改的)

    下面是一个示范:

    commit eb0b56b19017ab5c16c745e6da39c53126924ed6
    Author: Pieter Wuille <pieter.wuille@gmail.com>
    Date:   Fri Aug 1 22:57:55 2014 +0200
    
       Simplify serialize.h's exception handling
    
       Remove the 'state' and 'exceptmask' from serialize.h's stream
       implementations, as well as related methods.
    
       As exceptmask always included 'failbit', and setstate was always
       called with bits = failbit, all it did was immediately raise an
       exception. Get rid of those variables, and replace the setstate
       with direct exception throwing (which also removes some dead
       code).
    
       As a result, good() is never reached after a failure (there are
       only 2 calls, one of which is in tests), and can just be replaced
       by !eof().
    
       fail(), clear(n) and exceptions() are just never called. Delete
       them.
    

    需要注意:在写body的时候,保证每行最多只有72个字符

    Footer


    footer是可选的,可以用来跟踪issue的id,比如这个commit是用来关闭了某个issue:

    Closes #123
    

    Commitizen


    可以使用commitizen来帮助团队规范化整个流程,与npm scripts配合自动化整个流程。

    首先,本地安装Commitizen

    npm install commitizen --save-dev
    

    然后安装cz-conventional-changelogcz-conventional-changelog是一个适配器,用来适配不同代码的提交。这里我们项目中使用的是angular的代码适配器

    npm install cz-conventional-changelog --save-dev
    

    然后在package.json的root添加

    "config": {
        "commitizen": {
          "path": "cz-conventional-changelog"
        }
      }
    

    现在便可以使用Commitizen来提交git commit,具体用法就是,在git commit的时候改为使用git-cz.这里我把命令写进npm script中

    "scripts": {
        "commit": "git-cz"
      }
    

    然后我们在提交代码时先git add -A,然后npm run commit,就会在命令行提示输入header(type, scope, subject), body, footer

    相关文章

      网友评论

          本文标题:Git提交消息

          本文链接:https://www.haomeiwen.com/subject/rlempxtx.html