pre-commit is a tool that runs the checks listed in a repository's .pre-commit-config.yaml on the files you're about to commit, and stops the commit if any check fails. The config file is committed with the code, so everyone who works on the repository runs the same checks, at the same versions. See the pre-commit documentation. The commands here were run with pre-commit 4.7.0 and Git 2.50.1.

What problem does it solve?

You're working on my-app/, a small Python project. CI runs Black, a code formatter, and fails if any file isn't formatted. You commit an app.py with single quotes, a missing blank line, and spaces at the end of a line. You find out a few minutes after you push, and fix it in a second commit.

Git can run a check before the commit is made. A Git hook is a script that Git runs at a fixed point. The pre-commit hook runs before Git creates a commit, and if it exits with an error, Git makes no commit. See the githooks documentation. The tool is named after this hook. But hooks live in .git/hooks/, which isn't part of the repository, and cloning doesn't copy them. Every developer has to write the script and install the tools it calls.

pre-commit puts the list of checks in a file you commit, and installs the tools itself.

How do I set it up?

Install pre-commit with pip install pre-commit, or with uv tool install pre-commit if you use uv. Then add .pre-commit-config.yaml to the root of my-app/:

repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v6.0.0
    hooks:
      - id: trailing-whitespace
      - id: end-of-file-fixer
      - id: check-yaml
  - repo: https://github.com/psf/black-pre-commit-mirror
    rev: 26.10.1
    hooks:
      - id: black

Each item under repos is a Git repository that publishes hooks. rev is the tag to use, and each id picks one hook from that repository. Three general checks come from pre-commit-hooks: trailing-whitespace removes spaces at the ends of lines, end-of-file-fixer makes each file end with exactly one newline, and check-yaml checks that YAML files parse. The black hook comes from Black's mirror repository, as in Black's pre-commit setup. Each hook also takes options such as args, files, exclude, and types, which add arguments or choose the files it runs on. See the hook options.

Then turn the hook on in your clone:

pre-commit install
pre-commit installed at .git/hooks/pre-commit

This writes a short script to .git/hooks/pre-commit that starts pre-commit. Now commit the new app.py together with the config:

git add app.py .pre-commit-config.yaml
git commit -m "Add greeting"

The first run takes a while, because pre-commit clones both repositories and installs each hook's tool in its own environment, under ~/.cache/pre-commit. Later runs reuse them. Then the hooks run, in the order they appear in the config:

trim trailing whitespace..............................Failed
- hook id: trailing-whitespace
- exit code: 1
- files were modified by this hook

Fixing app.py

fix end of files......................................Failed
- hook id: end-of-file-fixer
- exit code: 1
- files were modified by this hook

Fixing app.py

check yaml............................................Passed
black.................................................Failed
- hook id: black
- files were modified by this hook

reformatted app.py

All done! ✨ 🍰 ✨
1 file reformatted.

pre-commit counts a hook as failed if it exits with an error or changes a file. Three hooks fixed app.py, so Git made no commit. The fixes are in your working tree, but not staged. Look at them with git diff, then stage the file and commit again:

git add app.py
git commit -m "Add greeting"
trim trailing whitespace..............................Passed
fix end of files......................................Passed
check yaml............................................Passed
black.................................................Passed
[main e1e47cb] Add greeting
 2 files changed, 16 insertions(+)
 create mode 100644 .pre-commit-config.yaml
 create mode 100644 app.py

What happens during a commit?

Figure 1 shows the steps of one commit.

Flow of git commit with pre-commit installed. You run git commit in my-app/, and Git runs .git/hooks/pre-commit. pre-commit stashes unstaged changes, then runs each hook on the staged files that the hook matches, reading .pre-commit-config.yaml and the hook tools in ~/.cache/pre-commit. If every hook passed, unstaged changes are put back and Git creates the commit. If a hook failed or changed a file, unstaged changes are put back and Git makes no commit, and you review the fixes, run git add, and commit again.
Figure 1. A hook that changes a file stops the commit even if it exits without an error, so a formatter that fixes everything still makes you commit twice.

The hooks check the staged version of each file, not the one on disk. If you've edited a file without staging the edit, pre-commit sets the edit aside first and puts it back afterwards. Suppose you stage a new util.py, while app.py has a line you haven't staged that ends in spaces:

git add util.py
git commit -m "Add farewell"
[WARNING] Unstaged files detected.
[INFO] Stashing unstaged files to ~/.cache/pre-commit/patch1796207392-33405.
trim trailing whitespace..............................Passed
fix end of files......................................Passed
check yaml........................(no files to check)Skipped
black.................................................Passed
[INFO] Restored changes from ~/.cache/pre-commit/patch1796207392-33405.
[main d793c29] Add farewell
 1 file changed, 2 insertions(+)
 create mode 100644 util.py

The spaces in app.py weren't checked, because they aren't part of this commit. check yaml was skipped because no YAML file was staged.

Can a hook use my project's own tools?

As written, the config makes pre-commit install its own copy of Black, at the version in rev. If my-app/ is a uv project with Black as a development dependency (uv add --dev black), the hook can run that copy instead. Replace Black's repository with a local hook:

repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v6.0.0
    hooks:
      - id: trailing-whitespace
      - id: end-of-file-fixer
      - id: check-yaml
  - repo: local
    hooks:
      - id: black
        name: black
        entry: uv run black
        language: unsupported
        types: [python]

repo: local means the hook is defined in this file rather than in a hook repository, so it needs a name and an entry, the command to run. pre-commit adds the staged file names to the end of entry. language: unsupported tells pre-commit to install nothing and run entry as it is. Before pre-commit 4.4.0 this was called language: system, which still works but will be removed. types: [python] limits the hook to Python files. See repository local hooks.

The hook now uses the Black version in uv.lock, the same one as uv run black. In exchange, everyone needs uv installed, since pre-commit no longer installs the tool.

What should I watch out for?

  • Hooks run only in clones where someone ran pre-commit install. The config file is shared, but .git/hooks/ isn't. Run pre-commit run --all-files in CI as well, so the checks don't depend on each developer's setup. See usage in CI.
  • git commit --no-verify skips every hook. If one hook is wrong for a commit, skip only that one by its id: SKIP=black git commit -m "Add shout".
  • A new hook checks only the files you change. Problems already in other files turn up later, mixed into unrelated commits. After adding or updating a hook, run pre-commit run --all-files once and commit the fixes on their own.
  • rev must be a tag or a commit, not a branch. pre-commit installs each rev once and never updates it, so a branch name stays at the commit it pointed to on the first run. pre-commit warns that such a rev "appears to be a mutable reference". To move each repository to its newest tag, run pre-commit autoupdate. See using the latest version.
  • The hook's version of a tool can differ from your project's. If pyproject.toml pins Black 25.12.0 and the config says 26.10.1, the two can format the same file differently, because Black may change its style in the first release of each year. Keep the versions in step, or use a local hook.
  • Commits fail if pre-commit is gone. The hook script runs pre-commit from the environment you installed it in, or else from your PATH. If you delete that environment, every commit fails with `pre-commit` not found. Install pre-commit again, then run pre-commit install.

When is it worth it?

  • Shared repositories with a formatter or linter. Everyone runs the same checks at the same versions, and problems are fixed before they reach a commit, a review, or CI.
  • Fast checks. Hooks run on every commit, so keep them to checks that take seconds, such as formatters, linters, and file checks. Leave the test suite to CI.

If CI already runs a formatter or linter, add a .pre-commit-config.yaml with the same tools at the same versions, run pre-commit run --all-files once, and keep the check in CI. For a script only you work on, with no checks to enforce, pre-commit adds setup without catching much.


Reading Time

~6 min read

Published

Category

Tags

Stay in Touch