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 - 知乎

相关推荐
头发那是一根不剩了4 小时前
Git 常用命令
git
x-cmd5 小时前
[x-cmd] x git - Git 命令增强工具
git·终端·命令行·x-cmd
程序员果子6 小时前
Git从零到远程协作:手把手实战指南
git
猫头虎10 小时前
OpenClaw 常用操作命令完整速查手册:终端 CLI 操作指令详解|聊天斜杠指令详情
运维·git·容器·开源·github·aigc·ai编程
小哈里1 天前
【工具】Linux远程开发核心工具,Git命令缩写与SSH常用命令
linux·git·ssh·工具·远程开发
迈克桀森1 天前
Git 日常操作全攻略:拉取 / 提交代码 + 高频命令速查
git·github
CCC:CarCrazeCurator1 天前
详解文件与文件夹权限:谁能操作、能做什么
git
Delta-delta1 天前
Git:warning: Clone succeeded, but checkout failed.
git
日光倾1 天前
【Vue.js 入门笔记】Git入门
笔记·git
dreams_dream2 天前
Git 的 Tag
git