git 报错 | husky - pre-commit hook exited with code 1 解决

背景:git commit 时出现【Husky + ESLint + Prettier 】依赖缺失/配置不一致等【代码规范问题】报错

原因【husky 的代码规范问题】 ESLint 配置里 extends: ['prettier'],但项目中没有正确安装 eslint-config-prettier,或版本被 pnpm 隔离导致 ESLint 找不到。

导致:husky 的 pre-commit → eslint --fix → 直接失败 → commit 被回滚

解决方案:

一、长期、稳定(推荐):

①安装缺失的依赖;

pnpm add -D eslint-config-prettier

②检查 ESLint 配置文件

  • .eslintrc.js

  • .eslintrc.cjs

  • .eslintrc.json

确保 extends

至少包含:

复制代码
extends: [
  'eslint:recommended',
  'plugin:vue/vue3-recommended',
  'plugin:@typescript-eslint/recommended',
  'prettier'
]

⚠️ 注意顺序

'prettier' 必须放在最后(官方要求)

③重新提交 (git comit )


二、立即提交,不被拦(临时方案)

法一:跳过 husky

git commit -m "xxx" --no-verify

适用场景
  • 正在大规模架构重构

  • 代码还不稳定

  • 不想被 lint / prettier 打断思路

tips:会跳过 husky

但 ESLint 问题依然存在,下次还会炸


法二:临时禁用 pre-commit 钩子)
方法 A:单次禁用(推荐)

HUSKY=0 git commit -m "架构重构"

--no-verify 等价

但更"工程化",有些团队更推荐这个

方法 B:暂时移走 hook(不推荐长期)

mv .husky/pre-commit .husky/pre-commit.bak git commit -m "架构重构"

提交完成后再恢复:

mv .husky/pre-commit.bak .husky/pre-commit


法三(只对你本地生效,长期重构期可用)

如果你接下来一段时间都不想被 lint 卡住

git config core.hooksPath .git/hooks-disabled

提交:git commit -m "架构重构"

恢复:git config --unset core.hooksPath


工作流建议:1. 重构完成 ;2. 结构稳定后 ,修 ESLint/Prettier,打开 husky;3. 最终提交: pnpm lint 和pnpm format


具体报错:

bash 复制代码
git commit -m 'xx'
✔ Preparing...
⚠ Running tasks...
  ❯ Running tasks for *.{js,ts,vue}
    ✖ eslint --fix [FAILED]
  ↓ No staged files match *.scss [SKIPPED]
↓ Skipped because of errors from tasks. [SKIPPED]
✔ Reverting to original state because of errors...
✔ Cleaning up...

✖ eslint --fix:

Oops! Something went wrong! :(

ESLint: 8.57.1

ESLint couldn't find the config "prettier" to extend from. Please check that the name of the config is correct.

The config "prettier" was referenced from the config file in "D:\xxx\node_modules\.pnpm\eslint-plugin-prettier@4.2.1_eslint@8.57.1_prettier@2.8.8\node_modules\eslint-plugin-prettier\eslint-plugin-prettier.js".

If you still have problems, please stop by https://eslint.org/chat/help to chat with the team.

husky - pre-commit hook exited with code 1 (error)

参考:【已解决】使用 husky、commitlint 后 git commit 报错:No staged files match any configured task.-CSDN博客git commit前检测husky与pre-commit - 知乎

相关推荐
活宝小娜15 小时前
git windows安装教程
git
青春喂了后端17 小时前
Go Sidecar Repository 并发锁改造:让并发请求安全地进入 Git 仓库层
git·安全·golang
小雨青年17 小时前
GitHub Actions 工作流性能优化实战,先看瓶颈,再改缓存和并发
git
0x000718 小时前
Git Bash 中无法启动 Claude Code ?
开发语言·git·bash
xuhaoyu_cpp_java18 小时前
Git学习(六)
git·学习
happyness441 天前
Git:AI编程时代的“安全带“与“时光机“
git·ai编程
To_OC1 天前
踩坑无数!终于捋顺Git基础核心工作流(新手必看)
git·程序员
xuhaoyu_cpp_java2 天前
Git学习(三)
经验分享·笔记·git·学习
C137的本贾尼2 天前
Git基本操作(二):add与commit,把文件交给Git管理
git
咸鱼永不翻身2 天前
Git Hooks—提交Commit前检查本地时间是否不对
git·git-hooks·git钩子