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.
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
- Run
pre-commit run --all-fileslocally before pushing. - Commit the reformatted files so CI has nothing left to change.
- Push again; the formatting hooks now pass with no modifications.
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.
- run: pre-commit run --all-files --show-diff-on-failureHow to prevent it
- Install the git hook locally (
pre-commit install) so formatting runs before every commit. - Use
--show-diff-on-failurein CI so the needed change is copy-pasteable. - Consider the pre-commit.ci bot to auto-commit fixes on pull requests.