git pre-commit & husky配置教程

用于git commit的前置检查,阻断不合格的git提交

分步指南
添加包含的步骤:

自动化安装husky,并安装lint-staged

npx husky-init && npm install
npm i lint-staged prettier -D
npx husky set .husky/pre-commit "npx lint-staged && npm run checktype"
npx husky set .husky/pre-push "npm test"

tip:

  • husky: 对git hooks进行了封装完善,更方便地调用git hooks;
  • lint-staged: 只检查暂存区的文件,速度更快;
  • prettier自动格式化代码
  • pre-push → npm test # push推送代码前,跑一遍测试用例

在package.json中添加配置

{
  "scripts": {
    ...
    "checktype": "tsc --noEmit"
  },
  ...
  "lint-staged": {
    "*.js": [
      "prettier --write"
    ],
    "*.ts?(x)": [
      "eslint",
      "prettier --parser=typescript --write"
    ]
  },
  ...
}

提交至git

git add -A && git commit -m "xxx" && git push

注意事项
Mac下或可能出现类似以下提示,husky钩子未生效:
hint: The ‘.husky/pre-commit’ hook was ignored because it’s not set as executable.
hint: You can disable this warning with git config advice.ignoredHook false.

这是文件权限问题,需要将脚本文件设置为可执行Unix文件:

chmod ug+x .husky/*

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值