What are Git hooks?

Why Interviewers Ask This

Interviewers use this question to quickly assess whether a candidate has the foundational knowledge required for Git & GitHub development. It reveals whether you understand the building blocks that more complex concepts rely on.

Answer

Git hooks are scripts that Git executes automatically before or after specific Git events, allowing you to automate tasks and enforce policies. They live in .git/hooks/ as executable scripts (shell, Python, Node.js, etc.). Hooks are not committed to the repo by default (they're in .git/) — to share hooks, use tools like husky (Node.js) or put them in a directory and symlink. Common client-side hooks: pre-commit — runs before a commit is created; used to run linters, formatters, tests. Exit non-zero to abort the commit; commit-msg — validates the commit message format (e.g., enforce conventional commits); pre-push — runs before pushing; used to run full test suite; post-commit — runs after commit (notifications); pre-rebase — can prevent rebasing certain branches. Server-side hooks (on the remote): pre-receive — validate pushes before accepting; enforce branch protection; post-receive — trigger deployments, send notifications after push. Tools: Husky — popular Node.js tool for managing Git hooks in package.json; lint-staged — run linters only on staged files.

Common Mistake

A common mistake is memorizing definitions without understanding implications. When asked this question, go one level deeper — explain what happens when this concept is misused or ignored.