How to run pre-commit using an interactive rebase
To run a shell command on each commit in an interactive rebase, use:
git rebase -i --exec "$command" $ref
which adds:
exec $command
after each commit in the rebase todo list.
You can edit or remove the commands if you like. You can even add exec lines
manually even if you didn’t start the rebase with the --exec option.
Running pre-commit hooks
To run your pre-commit hooks on each commit in an interactive rebase, use:
git rebase -i --exec "pre-commit run --hook-stage commit --from HEAD^ --to=HEAD" \
$ref
To run a command after every commit on your pull request branch without rebasing
your commits against a different ref, drop the -i flag:
git rebase --exec '$command' $ref
The rebase will stop if $command fails, so you can fix the issue and continue.
For a feature branch based on main, run:
git rebase --exec 'pre-commit run --hook-stage commit --from HEAD^ --to=HEAD' \
$(git merge-base main HEAD)
to check your pre-commit hooks pass on every commit.
This can be extended to verify that pre-commit hooks pass on the whole repo (not just the files modified in a commit) after each commit:
git rebase --exec 'pre-commit run --hook-stage commit --all-files' \
$(git merge-base main HEAD)
This is useful if you’ve rebased your pull request branch and want to verify that each commit is still atomic and passes all pre-commit checks.