Video in progress
How do you check that every commit in a PR compiles and passes tests on its own?
git rebase -i --exec "./mvnw -q test" origin/main runs the command after every commit and stops where it fails. I fix that commit, then git commit --amend and git rebase --continue.
📖 Answer
CI usually checks only the final state of the PR. The PR comes out green even if the intermediate commits do not compile. This is a problem in a team that uses "rebase and merge" or merge commits: broken commits stay in the history of main, and later git bisect trips over them.
git rebase -i --exec "./mvnw -q -o compile" origin/main
# or the short form: git rebase -x "./mvnw -q test" origin/main
In the todo list, an exec line appears after each pick:
pick 1a2b3c4 Invoice entity va repository
exec ./mvnw -q -o compile
pick 5d6e7f8 InvoiceService
exec ./mvnw -q -o compile
If the command exits with a non-zero code, the rebase stops at that commit. You fix it, git add, git commit --amend, then git rebase --continue. If you add --reschedule-failed-exec, the failed exec is run again afterwards, so you see that the fix really worked.
A practical tip: if a full test run takes 10 minutes per commit, 8 commits is an hour and a half. I usually limit myself to compile and fast unit tests, and leave the full test run for the last commit only.
If the team does a squash merge, this check matters less, since only one commit lands on main anyway.
Sign up to mark studied questions and save your progress.