别乱提交代码了,看下大厂 Git 提交规范是怎么做的!
架构师专栏 文章源自JAVA秀-https://www.javaxiu.com/43341.html
大家好,我是磊哥。文章源自JAVA秀-https://www.javaxiu.com/43341.html
Git是现在市面上最流行的版本控制工具,书写良好的commit message能大大提高代码维护的效率。但是在日常开发中由于缺少对于commit message的约束,导致填写内容随意、质量参差不齐,可读性低亦难以维护。在项目中引入commit message规范已是迫在眉睫。文章源自JAVA秀-https://www.javaxiu.com/43341.html
用什么规范?
现在市面上比较流行的方案是约定式提交规范
(Conventional Commits
),它受到了Angular提交准则
的启发,并在很大程度上以其为依据。约定式提交规范
是一种基于提交消息的轻量级约定。它提供了一组用于创建清晰的提交历史的简单规则;这使得编写基于规范的自动化工具变得更容易。这个约定与SemVer
相吻合,在提交信息中描述新特性、bug 修复和破坏性变更。它的 message 格式如下:文章源自JAVA秀-https://www.javaxiu.com/43341.html
注 意文章源自JAVA秀-https://www.javaxiu.com/43341.html
文末有:7701页互联网大厂面试题 文章源自JAVA秀-https://www.javaxiu.com/43341.html
<类型>[可选的作用域]: <描述>[可选的正文][可选的脚注]
Quick Start
1. 全局安装commitizen & cz-conventional-changelog
commitizen
是一个撰写合格commit message
的工具,用于代替git commit
指令,而cz-conventional-changelog
适配器提供conventional-changelog标准(约定式提交标准)。基于不同需求,也可以使用不同适配器。文章源自JAVA秀-https://www.javaxiu.com/43341.html
npm install -g commitizen cz-conventional-changelogecho '{ "path": "cz-conventional-changelog" }' > ~/.czrc
安装完毕后,可直接使用git cz
来取代git commit
。文章源自JAVA秀-https://www.javaxiu.com/43341.html
全局模式下,需要 ~/.czrc
配置文件, 为commitizen
指定Adapter
。文章源自JAVA秀-https://www.javaxiu.com/43341.html
2. 项目内安装commitlint & husky
commitlint
负责用于对commit message
进行格式校验,husky
负责提供更易用的git hook
。文章源自JAVA秀-https://www.javaxiu.com/43341.html
Use npm
文章源自JAVA秀-https://www.javaxiu.com/43341.html
npm i -D husky @commitlint/config-conventional @commitlint/cli
Use yarn
文章源自JAVA秀-https://www.javaxiu.com/43341.html
yarn add husky @commitlint/config-conventional @commitlint/cli -D
commitlint
只能做格式规范,无法触及内容。对于内容质量的把控只能靠我们自己。文章源自JAVA秀-https://www.javaxiu.com/43341.html
3. 添加相应配置
创建commitlint.config.js
文章源自JAVA秀-https://www.javaxiu.com/43341.html
# In the same path as package.jsonecho 'module.exports = {extends: ["@commitlint/config-conventional"]};' > ./commitlint.config.js
引入husky
文章源自JAVA秀-https://www.javaxiu.com/43341.html
# package.json...,"husky": { "hooks": { "commit-msg": "commitlint -e $GIT_PARAMS" }}
4. 使用
执行git cz
进入interactive模式,根据提示依次填写文章源自JAVA秀-https://www.javaxiu.com/43341.html
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>)
生成的commit message格式如下:文章源自JAVA秀-https://www.javaxiu.com/43341.html
<type>(<scope>): <subject><BLANK LINE><body><BLANK LINE><footer>
填写完毕后,husky
会调用commitlint
对message进行格式校验,默认规定type
及subject
为必填项。文章源自JAVA秀-https://www.javaxiu.com/43341.html
任何git commit
指令的option
都能用在 git cz
指令上, 例如git cz -a
文章源自JAVA秀-https://www.javaxiu.com/43341.html
Commit message规范在rrd-fe落地使用情况
针对团队目前使用的情况,我们讨论后拟定了commit message
每一部分的填写规则。文章源自JAVA秀-https://www.javaxiu.com/43341.html
1. type
type
为必填项,用于指定commit的类型,约定了feat
、fix
两个主要type
,以及docs、style、build、refactor、revert五个特殊type
,其余type
暂不使用。文章源自JAVA秀-https://www.javaxiu.com/43341.html
# 主要typefeat: 增加新功能fix: 修复bug# 特殊typedocs: 只改动了文档相关的内容style: 不影响代码含义的改动,例如去掉空格、改变缩进、增删分号build: 构造工具的或者外部依赖的改动,例如webpack,npmrefactor: 代码重构时使用revert: 执行git revert打印的message# 暂不使用typetest: 添加测试或者修改现有测试perf: 提高性能的改动ci: 与CI(持续集成服务)有关的改动chore: 不修改src或者test的其余修改,例如构建过程或辅助工具的变动
当一次改动包括主要type
与特殊type
时,统一采用主要type
。文章源自JAVA秀-https://www.javaxiu.com/43341.html
2. scope
scope
也为必填项,用于描述改动的范围,格式为项目名/模块名,例如:node-pc/common
rrd-h5/activity
,而we-sdk
不需指定模块名。如果一次commit修改多个模块,建议拆分成多次commit,以便更好追踪和维护。文章源自JAVA秀-https://www.javaxiu.com/43341.html
3. body
body
填写详细描述,主要描述改动之前的情况
及修改动机
,对于小的修改不作要求,但是重大需求、更新等必须添加body来作说明。文章源自JAVA秀-https://www.javaxiu.com/43341.html
4. break changes
break changes
指明是否产生了破坏性修改,涉及break changes的改动必须指明该项,类似版本升级、接口参数减少、接口删除、迁移等。文章源自JAVA秀-https://www.javaxiu.com/43341.html
5. affect issues
affect issues
指明是否影响了某个问题。例如我们使用jira时,我们在commit message
中可以填写其影响的JIRA_ID
,若要开启该功能需要先打通jira
与gitlab
。参考文档:docs.gitlab.com/ee/user/pro…文章源自JAVA秀-https://www.javaxiu.com/43341.html
填写方式例如:文章源自JAVA秀-https://www.javaxiu.com/43341.html
re #JIRA_IDfix #JIRA_ID
示例
完整的commit message示例文章源自JAVA秀-https://www.javaxiu.com/43341.html
文章源自JAVA秀-https://www.javaxiu.com/43341.html
相应的git log文章源自JAVA秀-https://www.javaxiu.com/43341.html
文章源自JAVA秀-https://www.javaxiu.com/43341.html
最后惯例,欢迎大家star我们的人人贷大前端团队博客,所有的文章还会同步更新到知乎专栏 和 掘金账号,我们每周都会分享几篇高质量的大前端技术文章。如果你喜欢这篇文章,希望能动动小手给个赞。文章源自JAVA秀-https://www.javaxiu.com/43341.html
扩展阅读
conventional commits 必读
介绍约定式提交标准。文章源自JAVA秀-https://www.javaxiu.com/43341.html
Angular规范 必读
介绍Angular标准每个部分该写什么、该怎么写。文章源自JAVA秀-https://www.javaxiu.com/43341.html
@commitlint/config-conventional 必读
介绍commitlint的校验规则config-conventional,以及一些常见passes/fails情况。文章源自JAVA秀-https://www.javaxiu.com/43341.html
近期技术热文文章源自JAVA秀-https://www.javaxiu.com/43341.html
文章源自JAVA秀-https://www.javaxiu.com/43341.html 字符串拼接,会走StringBuilder 吗?文章源自JAVA秀-https://www.javaxiu.com/43341.html
文章源自JAVA秀-https://www.javaxiu.com/43341.html 让SpringBoot不需要Controller、Service、DAO、Mapper,卧槽!这款工具文章源自JAVA秀-https://www.javaxiu.com/43341.html
文章源自JAVA秀-https://www.javaxiu.com/43341.html 免费的XShell替代品,又一款,国产良心工具....文章源自JAVA秀-https://www.javaxiu.com/43341.html
文章源自JAVA秀-https://www.javaxiu.com/43341.html Spring和SpringBoot 的区别是什么?文章源自JAVA秀-https://www.javaxiu.com/43341.html
第3版:互联网大厂面试题文章源自JAVA秀-https://www.javaxiu.com/43341.html
包括 Java 集合、JVM、多线程、并发编程、设计模式、算法调优、Spring全家桶、Java、MyBatis、ZooKeeper、Dubbo、Elasticsearch、Memcached、MongoDB、Redis、MySQL、RabbitMQ、Kafka、Linux、Netty、Tomcat、Python、HTML、CSS、Vue、React、JavaScript、Android 大数据、阿里巴巴等大厂面试题等、等技术栈!文章源自JAVA秀-https://www.javaxiu.com/43341.html
阅读原文: 高清 7701页大厂面试题 PDF文章源自JAVA秀-https://www.javaxiu.com/43341.html
阅读原文文章源自JAVA秀-https://www.javaxiu.com/43341.html

评论