Git hooks are scripts Git runs at events (pre-commit, pre-push). They are almost always Bash or sh-compatible—tie directly to the Git track.
Sample pre-commit
#!/usr/bin/env bash
set -e
if grep -R "FIXME" --include="*.py" .; then
echo "Remove FIXME before commit" >&2
exit 1
fi
echo "pre-commit ok"Place in .git/hooks/pre-commit and chmod +x—or use a hook manager like pre-commit framework.
pre-push smoke test
#!/usr/bin/env bash
npm test # example; replace with project commandKeep hooks fast—slow hooks get skipped with --no-verify, defeating the purpose.
Sharing hooks
Version hooks under scripts/hooks/ and document install steps—.git/hooks is not cloned. Teams symlink or copy on setup.
Important interview questions and answers
- Q: Where do hooks live?
A: In.git/hooks/with executable names matching hook names. - Q: exit 1 in a hook?
A: Aborts the Git operation (commit/push) with an error message.
Self-check
- What filename blocks commits in the example?
- Why keep hooks fast?
Tip: Version hook scripts in scripts/; .git/hooks is not cloned.
Interview prep
- Hook location?
.git/hooks with executable name.
- exit 1 in hook?
Aborts the Git operation with failure.