How to Fix Git Hook Not Executing Error
In this tutorial, you'll learn about How to Fix Git Hook Not Executing Error. We cover key concepts, practical examples, and best practices.
You installed a Git hook but it does not run — the hook script lacks execute permission, has the wrong filename, or Git is configured to use a different hooks directory.
The Problem
You created .git/hooks/pre-commit but committing still works without the hook running:
git commit -m "test"
No hook output is shown.
Step-by-Step Fix
Step 1: Check the hook filename
Hooks must have the exact filename:
| Hook | Filename |
|---|---|
| Pre-commit | pre-commit |
| Pre-push | pre-push |
| Post-commit | post-commit |
| Post-merge | post-merge |
| Pre-receive | pre-receive |
| Post-receive | post-receive |
# WRONG — wrong filename
.git/hooks/pre-commit.sh
# RIGHT
.git/hooks/pre-commit
Step 2: Make the hook executable
# WRONG — not executable
-rw-r--r-- 1 user user 1024 .git/hooks/pre-commit
# RIGHT — add execute permission
chmod +x .git/hooks/pre-commit
Verify:
ls -la .git/hooks/pre-commit
-rwxr-xr-x 1 user user 1024 .git/hooks/pre-commit
Step 3: Add the correct shebang
#!/bin/sh
Or:
#!/usr/bin/env bash
Step 4: Add a test line to verify execution
#!/bin/sh
echo "Hook is running!"
exit 0
Step 5: Check core.hooksPath
git config core.hooksPath
If set, Git uses that directory instead of .git/hooks/.
# If hooksPath points elsewhere, either move hooks or unset it
git config --unset core.hooksPath
Step 6: Use shared hooks directory
For team-shared hooks:
git config core.hooksPath .githooks
chmod +x .githooks/pre-commit
Commit the hooks directory to the repository.
Prevention Tips
- Always run
chmod +xafter creating hooks - Use
.githooksdirectory for team-shared hooks - Name hooks exactly as required (no extensions)
- Test hooks with
exit 1to verify they block - Use
git hook run <hook-name>to test hooks (Git 2.36+)
Common Mistakes with hook not running
- Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
These mistakes appear frequently in real-world GIT code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro