Lesson Completion
Back to course

What are Git Hooks?

Beginner
7 minutes4.7Git

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:

TimingHook TypeBehavior
Pre-Before the actionCan block it (exit non-zero)
Post-After the actionCannot block (informational)

Common hooks and when they fire:

HookEventCommon Use
pre-commitBefore commit is createdLint, format, test
commit-msgAfter message is writtenEnforce message format
pre-pushBefore push startsRun full test suite
post-commitAfter commit is createdNotifications
post-mergeAfter merge completesInstall dependencies
pre-rebaseBefore rebase startsWarn 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

flowchart LR ACTION["git commit"] --> PRE["🔍 pre-commit<br/>Lint, test, format"] PRE -->|"exit 0"| COMMIT["✅ Commit Created"] PRE -->|"exit 1"| BLOCKED["❌ Blocked"] COMMIT --> POST["📢 post-commit<br/>Notify"] style PRE fill:#1a1a2e,stroke:#ffd700,color:#ffd700 style BLOCKED fill:#2d1b1b,stroke:#e94560,color:#e94560 style COMMIT fill:#1b2d1b,stroke:#53d8fb,color:#53d8fb

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

bash
# ─── 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 message

Key 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 0 allows 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 (like pre-commit) can block the action by exiting with a non-zero code. Post-event hooks (like post-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.

Topics Covered

Git HooksAutomation

Tags

#git#hooks#automation#quality

Last Updated

2026-02-13