之前,发了一篇详细的Git使用教程:Git 从入门到精通,这篇超详细!包教包会!,十分详细,没看过的可以先收藏。今天,带大家看看大厂 Git 提交规范!值得学习!
-
用什么规范?
用什么规范?
<类型>[可选的作用域]: <描述>
[可选的正文]
[可选的脚注]
Quick Start
1. 全局安装commitizen & cz-conventional-changelog
npm install -g commitizen cz-conventional-changelog
echo '{ "path": "cz-conventional-changelog" }' > ~/.czrc
安装完毕后,可直接使用git cz
来取代git commit
。
全局模式下,需要 ~/.czrc
配置文件, 为commitizen
指定Adapter
。
2. 项目内安装commitlint & husky
commitlint
负责用于对commit message
进行格式校验,husky
负责提供更易用的git hook
。
Use npm
npm i -D husky @commitlint/config-conventional @commitlint/cli
Use yarn
yarn add husky @commitlint/config-conventional @commitlint/cli -D
commitlint
只能做格式规范,无法触及内容。对于内容质量的把控只能靠我们自己。
3. 添加相应配置
# In the same path as package.json
echo 'module.exports = {extends: ["@commitlint/config-conventional"]};' > ./commitlint.config.js
# package.json
...,
"husky": {
"hooks": {
"commit-msg": "commitlint -e $GIT_PARAMS"
}
}
4. 使用
执行git cz
进入interactive模式,根据提示依次填写
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>)
<type>(<scope>): <subject>
<BLANK LINE>
<body>
<BLANK LINE>
<footer>
填写完毕后,husky
会调用commitlint
对message进行格式校验,默认规定type
及subject
为必填项。
任何git commit
指令的option
都能用在 git cz
指令上, 例如git cz -a
Commit message规范在rrd-fe落地使用情况
针对团队目前使用的情况,我们讨论后拟定了commit message
每一部分的填写规则。
1. type
type
为必填项,用于指定commit的类型,约定了feat
、fix
两个主要type
,以及docs、style、build、refactor、revert五个特殊type
,其余type
暂不使用。
# 主要type
feat: 增加新功能
fix: 修复bug
# 特殊type
docs: 只改动了文档相关的内容
style: 不影响代码含义的改动,例如去掉空格、改变缩进、增删分号
build: 构造工具的或者外部依赖的改动,例如webpack,npm
refactor: 代码重构时使用
revert: 执行git revert打印的message
# 暂不使用type
test: 添加测试或者修改现有测试
perf: 提高性能的改动
ci: 与CI(持续集成服务)有关的改动
chore: 不修改src或者test的其余修改,例如构建过程或辅助工具的变动
当一次改动包括主要type
与特殊type
时,统一采用主要type
。
2. scope
3. body
body
填写详细描述,主要描述改动之前的情况
及修改动机
,对于小的修改不作要求,但是重大需求、更新等必须添加body来作说明。
4. break changes
break changes
指明是否产生了破坏性修改,涉及break changes的改动必须指明该项,类似版本升级、接口参数减少、接口删除、迁移等。
5. affect issues
re #JIRA_ID
fix #JIRA_ID
示例
最后惯例,欢迎大家star我们的人人贷大前端团队博客,所有的文章还会同步更新到知乎专栏 和 掘金账号,我们每周都会分享几篇高质量的大前端技术文章。如果你喜欢这篇文章,希望能动动小手给个赞。
文章来自:java进阶架构