Git & GitHub Interview Question #126
What is a Git hook?
Security, Team Workflows, Troubleshooting & Senior Git/GitHub Scenarios Senior Advanced
Quick Interview Answer
A Git hook is a script that Git can execute automatically at specific points in a repository workflow, such as before a commit, after a commit, before a push, or during server-side receive operations.
Detailed Explanation
A Git hook is a script that Git can execute automatically at specific points in a repository workflow, such as before a commit, after a commit, before a push, or during server-side receive operations.
Hooks can enforce local checks, format code, validate commit messages, or trigger automation. Client-side hooks are normally stored locally and are not automatically transferred when a repository is cloned, so important organizational rules should also be enforced in shared CI or server-side controls.
Code Example
# Common local hooks live under:
.git/hooks/
# Example hook names:
pre-commit
commit-msg
pre-push
# Git can also use a configured hooks path:
git config core.hooksPath .githooks