本文参考了 AngularJS Git Commit Message Conventions
以及 Conventional Commits, Conventional Commits 中文

# Git Commit 规范

良好的,遵循一定规则的提交信息不仅有助于编码历史的回顾,也有助于 CHANGELOG 等文件的生成。

# 格式 Format

每次提交,Commit message 都包括三个部分:HeaderBodyFooter。其中,Header 是必需的,BodyFooter 可以省略。

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

不管是哪一个部分,任何一行都不得超过 72 个字符(或 100 个字符)。Header 部分只有一行,包括三个字段: type (必需)、 scope (可选)和 subject (必需)。Footer 部分如果提交存在一个与之关联的 Issue 则要关联。

示例:

docs(changelog): update changelog to beta.5
fix(release): need to depend on latest rxjs and zone.js
The version in our package.json gets copied to the one we publish, 
and users need the latest of these.

# 主题 Subject

subject 是本次 commit 目的的简短描述,一般不要超过 50 个字符:

  • 使用祈使句和现在时:例如使用 "change" 而不是 "changed" 或 "changes"。
  • 规范大小写和相应书写规则。
  • 无需加句号符标识结尾。

# 类型 Type

类型是描述当前提交性质的枚举类型,含有以下的枚举值:

  • build: 用于修改项目构建系统,例如修改依赖库、外部接口或者升级 Node 版本等;
  • chore: 用于对非业务性代码进行修改,例如修改构建流程或者工具配置等;
  • ci: 用于修改持续集成流程,例如修改 Travis、Jenkins 等工作流配置;
  • docs: 用于修改文档,例如修改 README 文件、API 文档等;
  • feat: 增加特性;
  • fix: 修复 bug;
  • perf: 用于优化性能,例如提升代码的性能、减少内存占用等;
  • refactor: 用于重构代码,例如修改代码结构、变量名、函数名等但不修改功能逻辑;
  • style: 用于修改代码的样式,例如调整缩进、空格、空行等;
  • test: 用于修改测试用例,例如添加、删除、修改代码的测试用例等;
  • merge: 分支合并格式 -> orgin_branch into target_branch ;

如果 type 为 feat 和 fix,则该 commit 将肯定出现在 Change log 之中。其他情况(build、chore、ci、docs、perf、refactor、style、test、merge)由你决定,要不要放入 Change log,建议是不要。

# 作用域 Scope

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

# 主体 Body

Body 是对本地提交的一个详细描述,需要注意的是,描述每行字符不应超过 72 个字符,这利于各种环境下良好的阅读体验。

Footer 部分只用于两种情况。Breaking Changes(不兼容改动 and commit Closes(关闭 Issue.

1、不兼容变动

如果当前代码与上一个版本不兼容,则 Footer 部分以 BREAKING CHANGE 开头 —— Breaking Changes should start with the word BREAKING CHANGE: with a space or two newlines ,后面是对变动的描述、以及变动理由和迁移方法。

BREAKING CHANGE: isolate scope bindings definition has changed.
To migrate the code follow the example below:
Before:
scope: {
  myAttr: 'attribute',
}
After:
scope: {
  myAttr: '@',
}
The removed `inject` wasn't generaly useful for directives so there should be no code using it.

2、关闭或关联 Issue

如果当前 commit 针对某个 issue,那么可以在 Footer 部分关闭或关联这个 issue,或多个 issue 。

Closes #234
Closes #123, #245, #992
Ref #234

# 回退 Revert

还有一种特殊情况,如果当前 commit 用于撤销以前的 commit,格式规定如下

  • header 以 revert: 开头,后面跟着被撤销 commit 的 header
  • body 以 This reverts commit <hash> 的格式,其中的 hash 是被撤销 commit 的 SHA 标识符。
revert: feat(pencil): add 'graphiteWidth' option
This reverts commit 667ecc1654a317a13331b17617d973392f415f

如果当前 commit 与被撤销的 commit,在同一个发布(release)里面,那么它们都不会出现在 Change log 里面。如果两者在不同的发布,那么当前 commit,会出现在 Change log 的 Reverts 小标题下面。

A detailed explanation can be found in this document.

# 工具 Tools

Commitizen

commitizen 是一款用来生成规范化 commit 消息的工具。

## 安装 commitizen
$ npm install --save-dev commitizen
## 安装 你喜好的 commitizen adapter , 例如 cz-conventional-changelog 
$ npm install --save-dev cz-conventional-changelog
## 配置
## 在项目目录里,运行下面的命令,使其支持 Angular 的 Commit message 格式。
$ npx commitizen init cz-conventional-changelog --save-dev --save-exact
## 使用
## 用 git cz命令来取代 git commit

CommitLint

commitlint 是一款用来校验 commit 消息格式合法性的工具。

# 安装 CI
npm install --save-dev @commitlint/cli
# 安装 husky: 使 ghook 更容易
 --save-dev husky@next
# 配置 commitlint 使用 angular config
npm install --save-dev @commitlint/config-angular
echo "module.exports = {extends: ['@commitlint/config-angular']}" > commitlint.config.js
# 整合 commitizen
npm install --save-dev @commitlint/prompt
{
  "scripts": {
    "commit": "npx git-cz"
  },
  "config": {
    "commitizen": {
      "path": "./node_modules/@commitlint/prompt"
    }
  },
  "husky": {
    "hooks": {
      "commit-msg": "commitlint -e $GIT_PARAMS"
    }
  }
}

ChangeLog

# 安装 CI
npm install --save-dev conventional-changelog-ci
# 安装 standard-version: 自动生成 CHANGELOG 并发布版本
npm install --save-dev standard-version
{
  "scripts": {
    "changelog": "npx conventional-changelog -p angular -i CHANGELOG.md -s -r 0 && git add CHANGELOG.md",
    "release": "npx standard-version"
  }
}

# Git Emoji

关于 emoji 使用约定

主要场景是 changelogs 可在 git rebase 时时统一加上。
基础

# 🔨 / 📦build: 用于修改项目构建系统,例如修改依赖库、外部接口或者升级 Node 版本等

部署配置
🔨 build(config): Deploy the configuration
工具配置
🔧 build(tool): Tool configuration
删除依赖
❌ build(depend): Delete the dependency
新增依赖
⭕️ build(depend): Added dependencies
升级依赖
🔜 build(depend): Upgrade dependencies
降级依赖
🔙 build(depend): Downgrade dependencies

# 👷chore: 用于对非业务性代码进行修改,例如修改构建流程或者工具配置等

Jenkins配置
👷 chore(jenkins): Configuration
Docker修改持续集成
👷 chore(docker): Configuration

# 👷ci: 用于修改持续集成流程,例如修改 Travis、Jenkins 等工作流配置

Jenkins修改持续集成
👷 ci(jenkins): Modify continuous integration
Docker修改配置
👷 ci(docker): Modify the configuration by Docker

# 📝 / ✏️docs: 用于修改文档,例如修改 README 文件、API 文档等

完善文档
📝 docs(readme): Complete the documentation

# 🎉feat: 增加特性;

初始化项目
🎉 feat(init): Initialize the project
新增特性
✨ feat(function): New features
新增布局
✨ feat(layout): New layouts

# 🐛fix: 修复 bug

修复已知问题
🐛 fix(bug): Fix known issues

# ⚡️perf: 用于优化性能,例如提升代码的性能、减少内存占用等

代码性能优化
⚡️ perf(code): Code performance optimizations
资源性能优化
⚡️ perf(code): resource performance optimizations

# 💥refactor: 用于重构代码,例如修改代码结构、变量名、函数名等但不修改功能逻辑

不兼容更新
💥 refactor(incompatible): Incompatible updates
项目结构重构
🔥 refactor(structure): Structural restructuring
项目代码重构
🔥 refactor(code): Code refactoring

# 🎨 / 💎style: 用于修改代码的样式,例如调整缩进、空格、空行等

代码格式化
💎 style(code): Code formatting
样式代码
🎨 style(css): Style code

# 🔬 / 🚨test: 用于修改测试用例,例如添加、删除、修改代码的测试用例等

添加测试用例
🔬 test(add): Add a test case
删除测试用例
🔬 test(delete): Delete the test case
修改测试用例
🔬 test(modify): Modify the test case

#merge: 分支合并

➕ merge: orgin_branch into target_branch

# ♻️revert: 用于版本回退

♻️ revert: Revert to commit <hash>

过去无法挽回,未来可以改变,有的人成日殚精竭虑,却掀不起什么风浪,有的人却因一念之差,让世界天翻地覆,这就是命运权重。

阅读次数

请我喝[茶]~( ̄▽ ̄)~*

NIDLH 微信支付

微信支付

NIDLH 支付宝

支付宝