如何规范 Git Message

673 阅读2分钟

为什么要关注提交信息

  • 加快 Reviewing Code 的过程
  • 帮助我们写好 release note
  • 5年后帮你快速想起来某个分支,tag 或者 commit 增加了什么功能,改变了哪些代码
  • 让其他的开发者在运行 git blame 的时候想跪谢
  • 总之一个好的提交信息,会帮助你提高项目的整体质量
  • 更专业化管理好你的项目,提高项目完整性

Angular 规范的 Commit message 格式(正统规范)

每次提交,Commit message 都包括包括三个字段:type(必需)、scope(可选)和 subject(必需)。

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

  • WIP work in progress 向前推进
  • feat 新功能(feature)
  • fix 修补bug
  • docs 文档(documentation)
  • style 格式(不影响代码运行的变动)
  • refactor 重构(即不是新增功能,也不是修改bug的代码变动)
  • test 增加测试
  • chore 构建过程、辅助工具的变动
  • revert 回退一次提交

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

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

  • 以动词开头,使用第一人称现在时,比如 change,而不是 changed 或 changes
  • 第一个字母大写
  • 结尾不加句号

Body

Body 部分是对本次 commit 的详细描述,可以分成多行。下面是一个范例。

More detailed explanatory text, if necessary.  Wrap it to
about 72 characters or so.

Further paragraphs come after blank lines.

- Bullet points are okay, too
- Use a hanging indent

准备工作

确保自己环境已安装 Node, 本次主要依赖两个 Node 工程,npm install -g commitizen 全局安装 commitizen 工程。 npm i -g cz-customizable 全局安装 cz-customizable 工程。 同时在~/ 或项目目录下创建 .cz-config.js 文件, 拷贝下面代码到该文件。

'use strict';

module.exports = {

  types: [
    { 
      value: 'WIP',
      name : '💪  WIP:      Work in progress'
    },
    { 
      value: 'feat',
      name : '✨  feat:     A new feature'
    },
    { 
      value: 'fix',
      name : '🐞  fix:      A bug fix'
    },
    { 
      value: 'refactor',
      name : '🛠   refactor: A code change that neither fixes a bug nor adds a feature'
    },
    { 
      value: 'docs',
      name : '📚  docs:     Documentation only changes'
    },
    { 
      value: 'test',
      name : '🚨  test:     Add missing tests or correcting existing tests'
    },
    { 
      value: 'chore',
      name : '🗯   chore:    Changes that don\'t modify src or test files. Such as updating build tasks, package manager'
    },
    { 
      value: 'style',
      name : '💅  style:    Code Style, Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)'
    },
    { 
      value: 'revert',
      name : '⏪  revert:   Revert to a commit'
    }
  ],

  scopes: [],

  allowCustomScopes: true,
  allowBreakingChanges: ["feat", "fix"]
};

删除 cz-customizable 工程中 questions.js 中 关于 footer 部分,认为暂时不需要(也可以注释掉,后续需要再加回来)。

上述共工作完成后,使用git cz 代替 git commit 提交你的代码。

实际效果

参考文献

ruby china 关于如何写好 message
github.com/leonardoana…
github.com/angular/ang…
阿里南京技术专刊