Git Hook Lifecycle: Choose the Right Event
What is a hook?
A hook is an executable program that Git invokes at a documented event. Think of it as a checkpoint in a workflow, not a tracked commit or a permanent policy. In an ordinary repository Git looks in its hooks directory unless core.hooksPath changes the location. The exact filename and executable permission matter. Only install code you have reviewed and trust.
Choose the event
For an ordinary git commit, pre-commit checks the proposed content; commit-msg receives the message-file path; post-commit runs after the commit exists. A failing pre-commit or commit-msg prevents the commit, but a failing post-commit cannot undo it. prepare-commit-msg runs before message editing; its failure can stop the commit. A pre-push runs on the sending side, while pre-receive runs on the receiving repository before accepting proposed reference updates. Different hooks have different argument and stdin contracts.
Inspect the active location
In the disposable lab, inspect the repository and the effective configured hook path. An unset core.hooksPath normally means the default hooks directory. A nonzero status from config --get for a missing key is not itself a hook failure. Do not run any hook from a repository you do not trust.
git rev-parse --git-dirgit config --show-origin --get core.hooksPathPredict the result
Suppose a check rejects a staged note. HEAD should remain unchanged; the staged content remains available for correction. A notification after commit may fail while HEAD has already advanced. The lab logs event names and checks the reference before and after each operation.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Git Hook Lifecycle: Choose the Right Event — knowledge check
Reason about Git hooks and automation safety.
Which event checks proposed content before an ordinary commit?
What does commit-msg receive?
What happens if post-commit returns nonzero?
Where does pre-receive run?
What does a hook filename identify?