Markus Löning

Software Engineering & Machine Learning

Run pre-commit retrospectively on all changed files in a PR

Posted at Oct 2, 2020 16:10:57 — Last modified at Dec 7, 2025 19:20:23

When I was developing sktime, we had set up automated checks for printing and formatting only after some time. Rather than fixing our entire code base in one go, we decided to roll out the changes slowly, and only enforce them for the changed files on a PR.

New contributors would sometimes forget to set up pre-commit and open a PR without following the linting and formatting rules.

In cases like this, you cannot run pre-commit on all the files in the repo. Instead, you need to run pre-commit retrospectively only on the changed files in the PR from a <feature-branch> into main. For this, first make sure that main is up-to-date, so that your local git state reflects the PR on the remote repository. If you are working with a fork, take a look at GitHub’s guide on how to sync a fork.

Then run the following:

git checkout <feature-branch>
pre-commit run --files $(git diff --name-only HEAD main)