Sub topic 11
Running heavier checks with pre-push
This chapter assumes you can write a pre-commit hook, know that hooks are executable files whose exit status decides the outcome, and have set up core.hooksPath. Here you will move slow checks out of pre-commit and into pre-push.
Why a second stage
A pre-commit hook runs on every commit, so it must finish in a second or two. Full test suites, type checks across a whole project, and build verification are too slow for that. If they run on every commit, people start using --no-verify and stop trusting the hooks.
pre-push runs once per git push, so it can afford more time. It is also the last local checkpoint before commits leave your machine.
When pre-push runs
Git runs pre-push after it has contacted the remote and learned which refs it has, but before it sends any objects. If the hook exits non-zero, the push is aborted and nothing is transferred.
What the hook receives
Git passes two arguments and writes data to standard input:
$1is the name of the remote, such asorigin.$2is the remote's URL.- Standard input has one line per ref being pushed, in the form
<local ref> <local sha> <remote ref> <remote sha>.
Two special values matter. A remote SHA of all zeros means the branch does not exist on the remote yet. A local SHA of all zeros means you are deleting the remote branch.
A pre-push hook
This hook skips deletions, rejects pushes that contain commits whose subject starts with WIP, and then runs the test suite.
#!/bin/sh
# .githooks/pre-push
remote="$1"
while read -r local_ref local_sha remote_ref remote_sha; do
# A local sha made only of zeros means a branch deletion. Nothing to check.
case "$local_sha" in
*[!0]*) ;;
*) continue ;;
esac
# Pick the commits that the remote does not have yet.
case "$remote_sha" in
*[!0]*) range="$remote_sha..$local_sha" ;;
*) range="$local_sha --not --remotes=$remote" ;;
esac
if git log --format=%s $range | grep -q '^WIP'; then
echo "pre-push: $local_ref contains WIP commits. Clean them up first." >&2
exit 1
fi
done
if ! ./scripts/test.sh; then
echo "pre-push: tests failed, push aborted." >&2
exit 1
fi
exit 0Save it as .githooks/pre-push and run chmod +x .githooks/pre-push. Without the executable bit, Git silently skips it.
The range is left unquoted on purpose, so the shell splits it into separate arguments for git log. This is safe because the values are SHAs and a remote name.
Checking what is being pushed
The test run above checks your working tree, not the commits being pushed. If you have uncommitted edits, or you are pushing a branch that is not checked out, the tests can pass or fail for the wrong reason. Two common fixes:
- Require a clean tree by testing
git status --porcelainand aborting if it prints anything. - Check out the pushed commit in a temporary worktree and run the tests there.
dir=$(mktemp -d)
git worktree add --detach "$dir" "$local_sha" >/dev/null
(cd "$dir" && ./scripts/test.sh)
result=$?
git worktree remove --force "$dir"The second option is slower, but it tests exactly what will be pushed.
Choosing the stage
| Check | Stage | Reason |
|---|---|---|
| Whitespace, formatting, linting staged files | pre-commit | Fast and limited to changed files |
| Commit message format | commit-msg | Needs the message file |
| Full test suite, build, type check | pre-push | Slow, but needed once per push |
| Authoritative enforcement | CI | Cannot be skipped locally |
Limits
git push --no-verifyskips the hook, so treat it as a convenience and keep CI as the real gate.- Hooks run only on
git push. Pushes from a web interface or another tool never trigger them. - Output from slow tests is easy to lose. Print a clear final line saying which check failed.
Exercise
A developer deletes a remote branch with git push origin --delete old-feature. What does the pre-push hook receive for that ref, and why does the example hook skip it?
Show answer
The ref line has a local SHA made only of zeros, because there is no local commit to push. The hook skips it because there are no commits to inspect, and building a commit range from a zero SHA would produce an error.
Exercise
Why can a pre-push hook that simply runs ./scripts/test.sh give a misleading result?
Show answer
The script tests the current working tree. That tree can contain uncommitted changes or a different branch than the one being pushed, so the result may not describe the pushed commits. Requiring a clean tree or testing in a temporary worktree at the pushed SHA fixes this.