10月04, 2022

Git commit规范

如何保持git commit message 的规范,一直是我头痛的问题。最近在阅读源码的时候,发现koathinkjscommit message都符合一定的规范,于是便查找资料,写下这篇博客。文章内容包括git commit规范、git commit实例、相关插件和一些非常实用的链接。

格式

规范基本都是用的Angular规范这一套,比较明确。

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

type :用于表明我们这次提交的改动类型,是新增了功能?还是修改了测试代码?又或者是更新了文档?总结以下 11 种类型:

  1. build:主要目的是修改项目构建系统(例如 glup,webpack,rollup 的配置等)的提交。

  2. ci:主要目的是修改项目继续集成流程(例如 Travis,Jenkins,GitLab CI,Circle等)的提交

  3. docs:文档更新

  4. feat:新增功能

  5. fix:bug 修复

  6. perf:性能优化

  7. refactor:重构代码(既没有新增功能,也没有修复 bug)

  8. style:不影响程序逻辑的代码修改(修改空白字符,补全缺失的分号等)

  9. test:新增测试用例或是更新现有测试

  10. revert:回滚某个更早之前的提交

  11. chore:不属于以上类型的其他类型(日常事务)

optional scope:一个可选的修改范围。用于标识此次提交主要涉及到代码中哪个模块。

subject:一句话描述此次提交的主要内容,做到言简意赅。

其他body和footer介绍详情可查看:https://juejin.cn/post/7053730154710827045

更多详情可查看:https://docs.google.com/document/d/1QrDFcIiPjSLDn3EL15IJygNPiHORgU1_OOAqWjiDU5Y/edit#

实例

  1. Message Header。
chore: upgrade jackson-databind from 2.10.1 to 2.11.4
  1. Message Body。
fix(users): remove getFriends API endpoint
<br/>
In accordance with new privacy policy, remove the getFriends API endpoint. 
(The endpoint will continue to exist for a while and return a deprecation notice)
  1. Message Footer。
fix(users): remove getFriends API endpoint
<br/>
In accordance with new privacy policy, remove the getFriends API endpoint. 
(The endpoint will continue to exist for a while and return a deprecation notice)
<br/>
Implements #2105

插件

commitizen

commitizen 提供一个commit 格式化或交互工具, 最终需要输出符合 commit 规则的信息给 git。

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

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

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

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

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

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

参考链接:https://juejin.cn/post/7090177285252202527

husky + commitlint

husky可以让我们向项目中方便添加git hooks。

commitline和eslint类似,约束commit格式。

参考链接:https://www.cnblogs.com/cczlovexw/p/15789522.html

本文链接:https://imyoyo.xyz/post/git-commit-angular-standard.html

-- EOF --

Comments