The git-rebase option no-one knows that will shock your coworkers!
A few weeks ago, a moment before pushing upstream a glorious bug-fix, I ran the test suite locally, just to discover that I broke it!
Having lost all confidence in the commit history, I was out for blood, looking for the piece of code that ruined the whole PR!
With more than a hundred git aliases I consider myself quite a git nerd, so I reached out for a tool I knew:
My plan was:
- mark each commit with
- run the test suite with
bin/rakeat each stop
- identify the offending commit and fix whatever bugs I encountered
- amend the commit and call it a day
That didn’t feel right tho, it was too clunky, I had a hunch that I could have done better than this! So, after reading once again the
git rebase documentation, this little option popped out:
git rebase --exec!
--exec does to
The magic of
--exec is that it will execute a command after each commit in your interactive rebase.
If the command fails, it will stop the rebase, allow you to fix whatever needs to be fixed, and move along with
git rebase --continue.
This means that I was able to swiftly run this command after each commit and ensure that I had a green suite at every step of the PR.
git rebase --interactive --exec "bin/rake" origin/master
Don’t forget that this neat trick can be used for all kinds of problems, like, for example, running a linter for every commit of a PR.