用工具思路来规范化 git commit message

为何要规范 Git Commit Message

  • 发生问题快速识别问题代码
  • commit和代码创建联系,和相关prd、bug予以关联。

如何写出规范化的 Git Commit Message

选用当前业界内应用比较普遍的 Angular Git Commit Guidelineshtml

<type>(<scope>): <subject>
<BLANK LINE>
<body>
<BLANK LINE>
<footer>
  1. type:commit的类型:feat fix refactor docs style test chore
  2. scope:commit 影响的范围, 好比: route, component, utils, build...
  3. subject:commit 的概述node

    • 以动词开头,使用第一人称如今时,好比change,而不是changed或changes
    • 第一个字母小写(能够使用中文)
    • 结尾没有符号
  4. body:commit 具体修改内容, 能够分为多行
  5. footer:一些备注, 一般是 BREAKING CHANGE 或修复的 bug 的连接

项目使用参考步骤

1.Commitizen: 替代你的 git commit

commitizen/cz-cli, 咱们须要借助它提供的 git cz 命令替代咱们的 git commit 命令, 帮助咱们生成符合规范的 commit message.git

除此以外, 咱们还须要为 commitizen 指定一个 Adapter 好比: cz-conventional-changelog (一个符合 Angular团队规范的 preset). 使得 commitizen 按照咱们指定的规范帮助咱们生成 commit message.github

yarn add --dev commitizen cz-conventional-changelog

package.json中配置:npm

"script": {
    ...,
    "commit": "git-cz",
},
 "config": {
    "commitizen": {
      "path": "node_modules/cz-conventional-changelog"
    }
  }

若是全局安装过 commitizen, 那么在对应的项目中执行 git cz or npm run commit 均可以json

2.Commitlint: 校验你的 message

commitlint: 能够帮助咱们 lint commit messages,校验的配置推荐 [@commitlint/config-conventional]() (符合 Angular团队规范).
话很少说直接按照:ide

yarn add --dev @commitlint/config-conventional @commitlint/cli

同时须要在项目目录下建立配置文件 .commitlintrc.js, 写入:post

module.exports = {
  extends: [
    '@commitlint/config-conventional'
  ],
  rules: {
  }
};

3.结合 Husky

yarn add --dev husky

package.json 中添加:ui

"husky": {
    "hooks": {
      ...,
      "commit-msg": "commitlint -e $GIT_PARAMS"
    }
  },

参考资料

优雅的提交你的 Git Commit Messagecode

Commit message 和 Change log 编写指南

相关文章
相关标签/搜索