How to Fix Git Hook Failed (pre-commit, pre-push)
In this tutorial, you'll learn about How to Fix Git Hook Failed (pre. We cover key concepts, practical examples, and best practices.
The Problem
You run git commit and get error: pre-commit hook failed (add --no-verify to bypass) or husky > pre-commit hook failed. A Git hook script (linting, formatting, or testing) returned a non-zero exit code, preventing the commit or push. While hooks catch issues early, they can be frustrating when they block a legitimate commit due to a false positive or unrelated failure.
Quick Fix
1. Bypass the hook temporarily
# Skip pre-commit and commit-msg hooks
git commit --no-verify -m "message"
# Skip pre-push hooks
git push --no-verify
Use this sparingly — hooks exist for a reason.
2. View what the hook is doing
cat .git/hooks/pre-commit
If using husky:
cat .husky/pre-commit
3. Fix the underlying issue
# Lint errors
npx eslint . --fix
git add -A
# Formatting errors
npx prettier --write .
git add -A
# Test failures
npm test
# Fix failing tests before retrying the commit
4. Update husky hooks
# Reinstall husky
npx husky install
# Add a new hook
npx husky add .husky/pre-commit "npm run lint && npm test"
5. Make a hook non-blocking
# In .husky/pre-commit — add || true to make it informational
npm run lint || true
6. Debug a failing hook
# Run the hook script directly to see full output
sh .git/hooks/pre-commit
# Or with verbose
bash -x .git/hooks/pre-commit
7. Update hook scripts in .husky
# Edit the hook file
echo "npx lint-staged" > .husky/pre-commit
chmod +x .husky/pre-commit
Common Causes
| Cause | Error | Fix |
|---|---|---|
| Linter finds errors | ESLint: 5 errors, 3 warnings |
npx eslint . --fix |
| Formatting issues | Prettier: file needs formatting |
npx prettier --write . |
| Tests fail | Jest: 1 test failed |
Fix the failing test |
| Hook script has a bug | husky: pre-commit hook exited with code 1 |
Check .husky/pre-commit syntax |
| Pre-commit config wrong | .pre-commit-config.yaml syntax |
pre-commit run --all-files to debug |
| Missing dependencies | commitlint: command not found |
npm install or check hooks path |
Practice with a Test Repository
cd /tmp
mkdir git-practice && cd git-practice
git init --initial-branch=main
# Initialized empty Git repository in /tmp/git-practice/.git/
echo "test" > file.txt && git add . && git commit -m "init"
# [main (root-commit) abc1234] init
Before running destructive commands on your real repository, practice on a throwaway test repository. This builds confidence and prevents costly mistakes. The reflog is your safety net, but practice makes it less needed.
Prevention
- Run
npm run lintandnpm testbefore committing to catch issues early - Use
npx husky addto add hooks rather than editing.husky/*directly - Configure linters to auto-fix on save to reduce hook failures
- Use
lint-stagedto run linters only on staged files for faster hooks
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro