git rebasing an already pushed commit
I love git rebasing.
The default behaviour of git is to not allow you to rebase if you’ve already pushed to your origin, which makes sense because it could then make an alternate timeline for someone who has pulled your changes in the meantime.
But what if you really wanted to rebase it, and you’re certain that no-one has pulled the changes? Maybe you’re working on a personal project but you’re also pushing it to a remote server.
Thankfully there is a way to do it, and it’s pretty easy too.
git rebase -i origin/master~4 master
You’ll then be able to interactively rebase the last 4 commits as if you hadn’t pushed your changes. Change the number, change the amount of commits.
You should change the smallest number of commits as possible, as rebasing changes the commit hash, so any references to old hashes would be broken too.
Once you’ve done what you need to do, force push.
git push origin master
Hopefully you won’t need to do this often. But if you do, it’s good to know that it is possible, and don’t forget to use this carefully.