Skip to content
Latchkey

pre-commit "files were modified by this hook" fails the job in CI

A formatting hook changed files to fix them. pre-commit treats any file modification as a failure and exits non-zero with "files were modified by this hook", so the CI job goes red even though the fix succeeded.

What this error means

A hook like black, isort, prettier, or trailing-whitespace prints "Failed" with "- files were modified by this hook" and lists the reformatted files. Running again locally passes.

pre-commit
black....................................................................Failed
- hook id: black
- files were modified by this hook

reformatted src/app.py

All done!
1 file reformatted.

Common causes

Auto-fixing hooks report modification as failure

By design, formatters that rewrite files exit non-zero so a local commit gets re-staged. In CI there is no second commit, so the run stays failed.

The committed code was not formatted before pushing

The developer did not run the hooks locally, so CI is the first place the formatter touches the files.

How to fix it

Run the hooks and commit the fixes

  1. Run pre-commit run --all-files locally before pushing.
  2. Commit the reformatted files so CI has nothing left to change.
  3. Push again; the formatting hooks now pass with no modifications.
Terminal
pre-commit run --all-files
git add -u && git commit -m "style: apply pre-commit formatting"

Treat CI as a check gate, not an auto-fixer

In CI you want the formatter to fail if code is unformatted. Show the diff so the required change is obvious in the log.

.github/workflows/ci.yml
- run: pre-commit run --all-files --show-diff-on-failure

How to prevent it

  • Install the git hook locally (pre-commit install) so formatting runs before every commit.
  • Use --show-diff-on-failure in CI so the needed change is copy-pasteable.
  • Consider the pre-commit.ci bot to auto-commit fixes on pull requests.

Frequently asked questions

What causes ""files were modified by this hook""?
By design, formatters that rewrite files exit non-zero so a local commit gets re-staged. In CI there is no second commit, so the run stays failed.
How do I fix "files were modified by this hook"?
Run the hooks and commit the fixes

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card