Sub topic 10
Keeping hooks fast and choosing the right stage
This chapter assumes you can write a pre-commit hook, know that a non-zero exit status blocks the action, and know the difference between the index and the working tree. The question here is which check goes in which stage, and how to keep each stage cheap enough that people don't bypass it.
Why speed decides whether hooks survive
A pre-commit hook runs on every commit. If it takes 30 seconds, developers start committing with --no-verify, and the hook protects nothing. A rough budget:
- pre-commit: about 1 to 5 seconds. Developers commit often and wait for it.
- pre-push: tens of seconds. Pushes are rarer.
- CI: minutes. Nobody is waiting at a prompt.
Measure before you optimize:
time sh .githooks/pre-commitScope: check the change, not the repository
A hook's cost should grow with the size of the commit, not the size of the project. Ask Git for the staged file list and filter it by type. Exit early when nothing matches.
#!/bin/sh
# .githooks/pre-commit
# Lint only staged Python files. Assumes ruff is installed.
if ! git diff --cached --name-only --diff-filter=ACM -- '*.py' | grep -q .; then
exit 0
fi
git diff --cached --name-only --diff-filter=ACM -z -- '*.py' | xargs -0 ruff checkThe first command exits early on a docs-only commit. The second uses -z and xargs -0, so file names with spaces work. The pipeline's exit status is that of xargs, which is non-zero when ruff fails, so the commit is blocked.
This script lints the working-tree copies of the files. If you must check exactly what is staged, read each file with git show ":path", as in the earlier chapter on staged content.
Choosing the stage
Put each check at the earliest stage where it is both cheap and useful.
| Stage | Good fit | Poor fit |
|---|---|---|
| pre-commit | Formatting, linting changed files, whitespace errors, secret scanning of the diff | Full test suite, builds |
| commit-msg | Message format | Anything that needs the code |
| pre-push | Unit tests, type checking the whole project | Slow integration tests |
| CI | Everything, including slow and cross-platform checks | Nothing. It is the backstop |
A pre-push hook for a quick test run can be this short:
#!/bin/sh
# .githooks/pre-push
# Run the fast unit tests before sharing commits.
python -m pytest -q -xThe -x flag stops at the first failure, so a broken push fails quickly.
Techniques that keep hooks quick
- Filter by file type. A Python linter has no business running on a Markdown-only commit.
- Exit early. Check for matching files first, as in the script above.
- Run in parallel.
xargs -P 4runs up to four processes at once for tools that handle one file at a time. Parallel runs print interleaved output, so only use this when the tool's messages stay readable. - Use incremental tools. Linters with a cache skip unchanged files.
- Defer the expensive work. Move test suites to pre-push or CI instead of trimming them.
Common mistakes
- Auto-fixing in pre-commit. A formatter that rewrites files changes the working tree, but the commit still holds the old staged content. Either fail the commit and tell the developer to run the formatter, or re-stage the fixed files deliberately. Re-staging can pull in unstaged edits you did not mean to commit.
- Slow checks in the wrong stage. A five-minute test run in pre-commit trains people to skip hooks.
- Treating hooks as the gate. Hooks run on the developer's machine and can be skipped. Keep the same checks in CI so a bypassed hook does not let bad code through.
Exercise
A team's pre-commit hook runs the full test suite in two minutes, and developers have started using --no-verify. Which changes would you make, and which stage would each check move to?
Show answer
Keep only fast checks in pre-commit: formatting and linting of the staged files, with an early exit when no relevant files changed. Move the unit tests to pre-push so they run once per push instead of once per commit. Leave slow integration tests to CI, which also enforces every check for commits that skipped the hooks.