git commit message 规范

工具 刘宇帅 4年前 阅读量: 2103

为什么要规范 git commit message

无规矩不成方圆,尤其是在多人合作项目中如果每个人按自己的习惯 commit,那么整个 commit 的log将会一团糟糕

commit message 格式

Commit message 都包括三个部分:Header,Body 和 Footer

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

type

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

scope

commit 影响的范围, 比如: route, component, utils, build,scope 可以为空

subject

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

Body

commit 目的的简短描述

Footer

1. 不兼容改动
2. 关闭 Issue

commit 时自动检查 message 格式工具

在项目根目录安装依赖

npm install --save-dev @commitlint/cli  @commitlint/config-conventional
npm install --save-dev husky

添加文件 commitlint.config.js 并写入如下内容

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

在 package.json 根目录下添加如下内容

"husky": {
    "hooks": {
      "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
    }  
  }

完成配置,测试commit看下如下提示 message 不合法

➜  yundi-customer git:(master) ✗ git commit -m "test"
husky > commit-msg (node v12.14.0)
⧗   input: test
✖   subject may not be empty [subject-empty]
✖   type may not be empty [type-empty]

✖   found 2 problems, 0 warnings
ⓘ   Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint

husky > commit-msg hook failed (add --no-verify to bypass)

提示

功能待开通!


暂无评论~