别乱提交代码了,看下大厂 Git 提交规范是怎么做的!

沙海 2021年8月31日01:13:21Java评论37字数 3610阅读12分2秒阅读模式
摘要

别乱提交代码了,看下大厂 Git 提交规范是怎么做的! 架构师专栏

别乱提交代码了,看下大厂 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进行格式校验,默认规定typesubject为必填项。文章源自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的类型,约定了featfix两个主要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/commonrrd-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,若要开启该功能需要先打通jiragitlab。参考文档: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

别乱提交代码了,看下大厂 Git 提交规范是怎么做的!文章源自JAVA秀-https://www.javaxiu.com/43341.html

相应的git log文章源自JAVA秀-https://www.javaxiu.com/43341.html

别乱提交代码了,看下大厂 Git 提交规范是怎么做的!文章源自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

文章源自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

文章源自JAVA秀-https://www.javaxiu.com/43341.html

免费的XShell替代品,又一款,国产良心工具....文章源自JAVA秀-https://www.javaxiu.com/43341.html

文章源自JAVA秀-https://www.javaxiu.com/43341.html

文章源自JAVA秀-https://www.javaxiu.com/43341.html

Spring和SpringBoot 的区别是什么?文章源自JAVA秀-https://www.javaxiu.com/43341.html

文章源自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

继续阅读
速蛙云 - 极致体验,强烈推荐!!!购买套餐就免费送各大视频网站会员!快速稳定、独家福利社、流媒体稳定解锁!速度快,全球上网、视频、游戏加速、独立IP均支持!基础套餐性价比很高!这里不多说,我一直正在使用,推荐购买:https://www.javaxiu.com/59919.html
weinxin
资源分享QQ群
本站是JAVA秀团队的技术分享社区, 会经常分享资源和教程; 分享的时代, 请别再沉默!
沙海
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定