Programmers like to talk about “sharp tools”. Git is an excellent example of one.
Recently a colleague of mine accidentally pushed to the wrong repository (he somehow had the wrong remote set up). The good, and bad, thing about Git is that it doesn’t care. No warnings, no errors, just a merge commit.
To compound the problem, nobody noticed, and further commits were piled on top.
The first thing I did was make a new branch:
git checkout -b oops
And push it:
git push origin oops
That way, no matter what a mess I made of my local repo, I had a copy as it was originally.
I then rewound my repo to before the incident:
git checkout master git reset --hard INSERT_TREEISH_HERE
And pushed that (you have to force it, as it’s not FF):
git push -f origin master
Finally I cherry-picked the changes from the future:
git cherry-pick TREEISH_1 git cherry-pick TREEISH_2 git cherry-pick TREEISH_3
git push origin master
And, hopefully, we’re back to how we should have been :)