What are Git hooks and how do teams use them to enforce quality?
Take a moment.
Say your answer out loud or write it down first — retrieval is what makes it stick. Reveal when you're ready.
Short answer
Git hooks are scripts Git runs automatically at key points — pre-commit (lint/format before a commit lands), commit-msg (enforce message conventions), pre-push (run tests before sharing). They live in .git/hooks/ so they are not cloned; teams share them via a hooksPath config or tools like Husky to keep standards consistent across machines.
Detailed explanation
Hooks are executable scripts named after lifecycle events, stored in .git/hooks/ — Git runs them when the matching event fires and blocks the action if they exit non-zero. Practical pairings: pre-commit runs linters/formatters (ESLint, Prettier) so bad code never reaches the index; commit-msg validates message format (enforcing Conventional Commits) and rejects malformed ones; pre-push runs the test suite locally before the network round-trip. post-checkout can install dependencies after a branch switch. The catch: .git/hooks/ is not versioned, so a fresh clone starts empty. Teams solve this three ways — a core.hooksPath pointing to a tracked hooks/ directory, a prepare/install script that copies them, or Husky (which writes a tiny shim per hook and declares hooks in package.json). Combine with CI as the real gatekeeper — hooks are a fast local convenience, not a security boundary, since any developer can git commit --no-verify. Use them to save time and catch trivial mistakes, not to replace server-side checks.
Example
# Husky (declared in package.json) — shared via repo:
# .husky/pre-commit
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
npx lint-staged
# .husky/commit-msg
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
npx --no -- commitlint --edit ${1}
# Track hooks without Husky (git config):
git config core.hooksPath ./hooksInterview tip
List the three most useful hooks (pre-commit, commit-msg, pre-push) with concrete tools, then address the sharing problem — that is what separates book knowledge from team experience.
Common mistakes
- Forgetting hooks are local and not shared on clone.
- Treating hooks as a security boundary instead of a convenience.
- Forgetting developers can bypass with --no-verify.