How-To Guide

How to Implement a Pre-Commit Hook Pipeline

Use pre-commit hooks to catch problems early: run the fast checks on the staged files, keep them quick, and let the CI enforce the rest.

On this page

  1. Introduction
  2. Run the fast checks on the staged files
  3. Keep the hook fast
  4. Fail the commit on a problem
  5. Provide a deliberate bypass
  6. Test the hook and the bypass
  7. Common pitfalls
  8. Conclusion

Introduction

A pre-commit hook runs the checks before a commit, which is the cheapest time to fix a problem: the developer has the context and the fix is one command. A hook that catches a formatting or a lint error saves a CI cycle and the review comment. But a hook that is slow is disabled, so the art is to run only the checks that are fast and to leave the rest to the CI.

This tutorial implements a pre-commit hook: run the fast checks on the staged files, keep the hook fast, fail the commit on a problem, and test the hook and the bypass. The examples use a hook manager, and the design applies to any git hook.

Run the fast checks on the staged files

The pre-commit runs the formatters, the linters, and any fast unit tests, on the staged files, so only the files about to be committed are checked. Checking the whole repository is slow and checks files the developer did not touch, which is noise. The staged files are the relevant set, and checking them is fast and focused.

The checks should fix what they can, such as a formatter that rewrites the file, and report what they cannot, such as a lint error that the developer must fix. A hook that only reports makes the developer run the fix separately; a hook that formats the staged files in place and re-stages them is smoother. The hook is a convenience, so it should reduce the work, not add a step.

#!/bin/sh
# .git/hooks/pre-commit: run the fast checks on the staged files
files=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(ts|js|py)$')
[ -z "$files" ] && exit 0
npx prettier --write $files && git add $files   # format in place and re-stage
npx eslint $files || exit 1                       # fail the commit on a lint error

Keep the hook fast

The hook should complete in a few seconds, so the developer does not wait and does not disable it. The slow checks, such as the full test suite, the build, and the type check on a large project, belong in the CI, which runs them on every push and is the gate that cannot be bypassed. The pre-commit is the fast feedback, and the CI is the thorough one.

Measure the hook’s time and keep it under a few seconds; if a check pushes it over, move that check to the CI. The developers will bypass a hook that is painful, so a fast hook is the one that is actually used. The fast checks are the formatting and the linting, which are seconds; the testing and the build are the CI’s.

Fail the commit on a problem

When a check fails, the hook exits non-zero, which blocks the commit. The developer fixes the issue and commits again. A hook that warns but lets the commit through is not enforcing anything, because the commit lands in the history and the CI catches it later, at a higher cost. The non-zero exit is what makes the hook a gate.

The hook should print what failed and how to fix it, so the developer can act. It should not silently pass a failure, and it should not block on a check that is flaky or slow. A hook that fails for a reason the developer cannot fix, such as a network check, is a hook they disable, so keep the checks deterministic and local.

Provide a deliberate bypass

A hook should be bypassable for an emergency, with a flag such as git commit --no-verify, so a developer can commit when the hook is wrong or when the fix must wait. The bypass is deliberate: the developer knows the check is skipped, and the CI will catch it. A hook that cannot be bypassed is a hook that blocks on a false positive with no escape.

Document the bypass and discourage its routine use. The bypass is for the emergency, and the CI is the backstop, so a committed bypass is caught there. A team that bypasses routinely has a hook that is too strict or too slow, and the fix is to tune the hook, not to remove it.

Test the hook and the bypass

Test that a staged file with a lint error fails the hook, by introducing an error and running the hook, and asserting the non-zero exit. Test that a clean staged file passes. Test that the hook runs only on the staged files, by staging one file and asserting the others are not checked. Test the bypass, by asserting the commit succeeds with –no-verify.

# a lint error in a staged file fails the hook
echo "const x = 1" >> src/bad.ts && git add src/bad.ts
git commit -m test        # expect the hook to fail with a non-zero exit
git commit --no-verify -m test   # the bypass: the commit succeeds

Common pitfalls

A slow hook that the developers disable, checking the whole repository instead of the staged files, a hook that warns but does not fail so the commit lands anyway, a flaky or network check so the hook fails for an unfixable reason, and no bypass so a false positive blocks with no escape are the recurring mistakes. A fast check on the staged files, a non-zero exit, deterministic local checks, and a documented bypass address them.

Conclusion

A pre-commit hook catches the cheap-to-fix issues before the commit. Run the fast checks on the staged files, keep the hook under a few seconds, fail the commit on a problem, and provide a deliberate bypass. Self-check by introducing a lint error and asserting the hook fails, and by asserting a clean commit passes. Then the hook saves a CI cycle without slowing the developer.

Sources: pre-commit: A framework for managing git hooks, Git: Customizing git hooks, Husky: Git hooks.

FAQ

Why keep the hook fast?
A hook that takes a minute is disabled by the developers, which defeats its purpose. Keep the pre-commit to the checks that take a few seconds, such as a formatter and a linter on the changed files, and move the slow tests and the full build to the CI, which runs them anyway.
Pre-commit or CI?
Both, for different checks. The pre-commit catches the issues that are cheap to fix before the commit, such as a formatting or a lint error, and the CI enforces the full suite on every push. The pre-commit is a convenience for the developer; the CI is the gate that cannot be bypassed.

Related Examples