The Hook (The "Byte-Sized" Intro)
What if Git could automatically lint your code before every commit? Run tests before every push? Format your commit messages? Git hooks do exactly that — scripts that trigger automatically on Git events. They're your personal quality gates that run silently in the background, catching mistakes before they reach the team.
📖 What are Git Hooks?
Git hooks are executable scripts that run automatically when specific Git events occur (commit, push, merge, etc.). They can block actions (pre-hooks) or react to them (post-hooks).
Conceptual Clarity
Hook types:
| Timing | Hook Type | Behavior |
|---|---|---|
| Pre- | Before the action | Can block it (exit non-zero) |
| Post- | After the action | Cannot block (informational) |
Common hooks and when they fire:
| Hook | Event | Common Use |
|---|---|---|
pre-commit | Before commit is created | Lint, format, test |
commit-msg | After message is written | Enforce message format |
pre-push | Before push starts | Run full test suite |
post-commit | After commit is created | Notifications |
post-merge | After merge completes | Install dependencies |
pre-rebase | Before rebase starts | Warn about shared branches |
Real-Life Analogy
Hooks are like an automatic spell checker in a document editor. Before you "submit" (commit), it checks for errors. If errors are found, it warns you and can prevent submission. After you submit, it might notify your collaborators.
Visual Architecture
Why It Matters
- Automation: Quality checks run without manual effort.
- Prevention: Catch issues before they reach the remote repo.
- Consistency: Every commit goes through the same checks.
- Speed: Fast local checks are faster than waiting for CI.
Code
# ─── See available hooks ───
ls .git/hooks/
# pre-commit.sample commit-msg.sample pre-push.sample ...
# ─── Enable a hook (remove .sample) ───
cp .git/hooks/pre-commit.sample .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit
# ─── Simple pre-commit hook ───
cat > .git/hooks/pre-commit << 'EOF'
#!/bin/sh
# Run linter before commit
npm run lint
if [ $? -ne 0 ]; then
echo "❌ Lint failed. Fix errors before committing."
exit 1
fi
EOF
chmod +x .git/hooks/pre-commit
# ─── How blocking works ───
# exit 0 → action proceeds
# exit 1 → action is blocked with error messageKey Takeaways
- Git hooks are scripts that run automatically on Git events.
- Pre-hooks can block actions; post-hooks can only react.
- Hooks live in
.git/hooks/and must be executable. exit 0allows the action; non-zero blocks it.
Interview Prep
-
Q: What are Git hooks and how do they work? A: Git hooks are executable scripts in
.git/hooks/that trigger on specific Git events. Pre-event hooks (likepre-commit) can block the action by exiting with a non-zero code. Post-event hooks (likepost-commit) run after the action completes. -
Q: Can hooks be bypassed? A: Client-side hooks can be bypassed with
--no-verify(e.g.,git commit --no-verify). Server-side hooks cannot be bypassed by the client — they enforce rules at the server level. -
Q: Why aren't hooks committed to the repo? A: The
.git/hooks/directory is not tracked by Git (it's inside.git/). This is by design — hooks are local and may contain machine-specific paths or preferences. Tools like Husky solve sharing by storing hooks in the working directory.