Removing a random set of git commits from a remote branch
The Situation
A developer (developer A) on your team commited to a branch you are working off of (staging
in this case), and you did not want the commits on staging
yet, as they were not ready for release. You accidently pulled, merged, and pushed (because you are an idiot and didnt catch it) and now you have polluted the global, remote staging
branch.
How do you remove the series of commits?
The Solution (non-conventional)
First, assuming you are sitting on the polluted staging
branch locally (run git branch
), create a new branch called old_staging
, branching off of staging
:
git checkout -b old_staging
Now all the commits your about to blow away are save locally at least. Next, find the last commmit (use git log
on staging
that was not polluted (basically the last commit before developer A’s commits). Use that git hash to reset HEAD
git reset --hard <LAST GOOD HASH>
Now, blow away his changes, but make sure he knows you’re doing it so he can save them locally also.
git push -f
Now your remote is no longer polluted, but any changes you made since
Finally, push your changes up to remote
git push
Boom - you’ve solved your problem, in a somewhat non-conventional way.
Disclaimer: you might only want to do this if:
- Your team is very small
- You own the company