使用第三方工具实现commit规范

痛点分析

  • 1:多人合作开发,commit注释,随机编写规范提交格式

  • 2:提交日志太多,无法查找(规范后可以过滤查找)

  • 3:读不懂别人的提交日志 掌握提交语法

用例

git commit -m '提交规范,这里的注释应该如何编写'

语法

git commit -m 'type(scope): subject'

commit有什么

  • head 标题行: 必填, 描述主要修改类型和内容

    • type 必填

    • scope 可选项,一般用户写入模块

    • subject 陈述信息

  • body  描述为什么修改, 做了什么样的修改, 以及开发的思路等等

  • footer 页脚注释: 放 Breaking Changes 或 Closed Issues

常用的只有head

head详解

关键字type说明
feat新增功能
fixbug修复
docs文档更新,![](file:///C:\Users\86182\AppData\Roaming\Tencent\QQ\Temp\%W@GJ$ACOF(TYDYECOKVDYB.png)README.md等
style不影响程序逻辑的代码修改(修改空白字符,格式缩进,补全缺失的分号等,没有改变代码逻辑)
refactor重构代码,不是修改不bug,也不是新增功能feart
perf性能, 体验优化
test新增测试用例或是更新已存在测试
build修改构建系统,例如(gulp rollup webpack)等配置文件修改
ci修改持续集成文件,例如:ravis,Jenkins,GitLab CI,Circl等提交
revert回退版本到某个更早提交
chore没有上述变动,其他;例如需修改test src目录 打包发布前,提交用chore
depc升级依赖

练习测试

例如:新增功能首页

 git commit -m 'feat(Home): 新增首页功能' 

例如:修改详情页bug

 git commit -m 'fix(Detail): 修改bug'

例如:打包发布button组件

git commit -m 'chore(all): 打包发布button组件'

过滤查找

git log --pretty=oneline --grep=feat

新痛点

描述:

1:每次手动输入 type scoped subject 。如果不细心有可能会出错,有没有工具,可以帮助创建提交信. 2:有没有工具检查只有正确提交信息才能commit

  • commitlizen 可以帮创建commit

安装工具和适配器(规范)

commitizen/cz-cli: 使用 git cz 命令替代我们的 git commit 命令, 帮助我们生成符合规范的 commit message

cz-conventional-changelog:为 commitizen 指定一个 Adapter(适配器)(一个符合 Angular团队规范的 preset). 使得 commitizen 按照我们指定的规范帮助我们生成 commit message.

 npm install -g commitizen
 npm install -g cz-conventional-changelog

项目中配置使用

在项目里安装:

!!!(项目里没有package.json文件的情况下)需要初始化一下

npm init
commitizen init cz-conventional-changelog --save --save-exact

 package.json文件,会发现多出一部分配置信息

安装 commitlint
commitlint: commit 提交格式校验工具

npm install -D @commitlint/config-conventional @commitlint/cli

package.json 同级 配置 commitlint.config.js 文件

module.exports = { extends: ["@commitlint/config-conventional"] };

安装 husky–配置 husky

    npm i husky -D

安装完后,需要配置package.json,往里面添加: 

    "husky":{
        "hooks": {
         "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
        }
    }

使用

    git cz

用 git cz 命令取代 git commit(先使用git add .),这时会出现如下选项:

( 1 )选择 type

? Select the type of change that you're committing: (Use arrow keys)
> feat:     A new feature
  fix:      A bug fix
  docs:     Documentation only changes
  style:    Changes that do not affect the meaning of the code (white-space,
formatting, missing semi-colons, etc)
  refactor: A code change that neither fixes a bug nor adds a feature
  perf:     A code change that improves performance
(Move up and down to reveal more choices)

( 2 )填写 scope(选填)

? What is the scope of this change (e.g. component or file name)? (press enter to skip)
core

( 3 )填写 subject 简短的的描述

? Write a short, imperative tense description of the change:

( 4 )提供更长的变更描述

? Provide a longer description of the change: (press enter to skip)

 ( 5 )有什么突破性的变化

? Are there any breaking changes? (y/N)  

( 6 )否会影响任何未决问题 

? Does this change affect any open issues? (y/N) 

 完成,运行 git log 命令,查看我们刚才提交的 commit message

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值