pre-commit "trailing-whitespace" modifies files and fails in CI
The trailing-whitespace hook removes trailing spaces from lines. Because it edits files, pre-commit reports "files were modified by this hook" and exits non-zero, failing the CI check.
What this error means
The hook prints "Failed" with "files were modified by this hook" and lists files it trimmed. Locally, running it and committing makes it pass.
pre-commit
Trim Trailing Whitespace...................................Failed
- hook id: trailing-whitespace
- exit code: 1
- files were modified by this hook
Fixing src/app.pyCommon causes
Committed files had trailing whitespace
The hook fixed the whitespace, which is a modification, so pre-commit fails in CI where there is no follow-up commit.
Hooks were not run locally before pushing
CI is the first place the whitespace hook touches the files, so it fails there instead of at commit time.
How to fix it
Run the hook and commit the fixes
- Run
pre-commit run trailing-whitespace --all-fileslocally. - Commit the trimmed files.
- Push again so CI has nothing to fix.
Terminal
pre-commit run trailing-whitespace --all-files
git add -u && git commit -m "chore: trim trailing whitespace"Show the diff so the fix is obvious in CI
Add --show-diff-on-failure so the whitespace change is visible in the log.
.github/workflows/ci.yml
- run: pre-commit run --all-files --show-diff-on-failureHow to prevent it
- Install the git hook locally so whitespace is trimmed at commit time.
- Use
--show-diff-on-failurein CI. - Configure editors to strip trailing whitespace on save.
Related guides
pre-commit "files were modified by this hook" fails the job in CIFix pre-commit "files were modified by this hook" in CI - an auto-fixing formatter (black, prettier, isort) r…
pre-commit "check-added-large-files" fails in CIFix pre-commit "check-added-large-files" failing in CI - a committed file exceeds the size limit, so the hook…
pre-commit only checks changed files in CI (missing --all-files)Fix pre-commit checking only changed files in CI - without --all-files pre-commit runs against the staged dif…