git 提交规范笔记

199 阅读2分钟

message 格式

  <type>(<scope>): <subject>
  // 空一行
  <body>
  // 空一行
  <footer>

全局安装 commitizen & cz-conventional-changelog

commitizen是一个撰写合格commit message的工具,用于代替git commit 指令,而cz-conventional-changelog适配器提供conventional-changelog标准(约定式提交标准)。基于不同需求,也可以使用不同适配器。

项目内安装 commitlint & husky

commitlint负责用于对commit message进行格式校验,husky负责提供更易用的git hook。

// commitlint.config.js
module.exports = {extends: ["@commitlint/config-conventional"]};
  // package.json
  "husky": {
    "hooks": {
      "commit-msg": "commitlint -e $GIT_PARAMS"
    }
  }

使用

执行git cz进入interactive模式,根据提示依次填写

  • Select the type of change that you're committing 选择改动类型 (<type>)

  • What is the scope of this change (e.g. component or file name)? 填写改动范围 (<scope>)

  • Write a short, imperative tense description of the change: 写一个精简的描述 (<subject>)

  • Provide a longer description of the change: (press enter to skip) 对于改动写一段长描述 (<body>)

  • Are there any breaking changes? (y/n) 是破坏性修改吗?默认n (<footer>)

  • Does this change affect any openreve issues? (y/n) 改动修复了哪个问题?默认n (<footer>)

参数说明

  1. type

    主要

    • feat: 增加新功能
    • fix: 修复bug

    特殊type

    • docs: 只改动了文档相关的内容
    • style: 不影响代码含义的改动,例如去掉空格、改变缩进、增删分号
    • build: 构造工具的或者外部依赖的改动,例如webpack,npm
    • refactor: 代码重构时使用
    • revert: 执行git revert打印的message

    暂不使用type

    • test: 添加测试或者修改现有测试
    • perf: 提高性能的改动
    • ci: 与CI(持续集成服务)有关的改动
    • chore: 不修改src或者test的其余修改,例如构建过程或辅助工具的变动
  2. scope

    scope用于说明 commit 影响的范围

  3. subject

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

  4. body

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

  5. break changes

    break changes指明是否产生了破坏性修改,涉及break changes的改动必须指明该项,类似版本升级、接口参数减少、接口删除、迁移等。

  6. affect issues

    affect issues指明是否影响了某个问题。 原文 zhuanlan.zhihu.com/p/88870009