A Real, Executable Hook
#!/bin/sh
if ! grep -qE "^[A-Z]+-[0-9]+: " "$1"; then
echo "ERROR: commit message must start with a ticket reference, e.g. 'RECIPE-123: ...'"
exit 1
fi
git passes the proposed commit message's real file path as $1. A non-zero exit genuinely blocks the commit.
A Real Rejection
git commit -m "add meal prep containers guide"
# ERROR: commit message must start with a ticket reference...
Rejected immediately — the real commit never happens, the staged changes stay staged.
The Same Real Change, Now Accepted
git commit -m "RECIPE-301: Add meal prep containers guide"
Identical content, real ticket prefix — the hook lets it straight through.
git runs a real script at .git/hooks/commit-msg (if one exists and is executable) every single time a commit is attempted, passing it the path to a real temp file holding the proposed message. If the script exits non-zero, the real commit is genuinely rejected — no exceptions, no way around it short of --no-verify.
This lab writes a real hook requiring every commit message to start with a ticket reference like RECIPE-123: . A real commit without one gets rejected on the spot; the identical real change, committed with the right prefix, goes through immediately.
commit-msg hook
A real, local script git runs automatically on every commit attempt, given the proposed message's file path. Confirmed directly in this lab: a non-zero exit genuinely blocked the commit before it was created.
Hooks are local, not shared
A hook committed inside a repository's tracked files is never automatically installed for anyone else — .git/hooks/ itself is never part of a real clone. Real teams distribute hooks separately (a setup script, or a tool like Husky).
Takeaways
- A commit-msg hook receives the proposed message's real file path and can genuinely reject the commit with a non-zero exit
- The rejection happens before the commit is created — staged changes are untouched, nothing is lost
- Hooks live in .git/hooks/ and are never part of a real clone — shared hooks need separate distribution
- --no-verify is the real, deliberate escape hatch when a hook genuinely needs to be bypassed