Space Vatican

Ramblings of a curious coder

Fixing an Accidental Git Push --force

I did something stupid with git today. I was working alone on a topic brand and had just rebased it against master. I did git push --force to push my changes to github. The output wasn’t quite what I expected:

 + e9c00be...2652a00 garment-quiz -> garment-quiz (forced update)
 + d91922d...2fec250 release-2012-07-04 -> release-2012-07-04 (forced update)

What’s the second update doing there? I had a local branch tracking our latest release branch and that local branch wasn’t upto date – I hadn’t pulled changes made by other people (but all of my commits had been previous pushed to the repo). When I forced pushed, git pushed everything it was tracking, overwriting colleagues commits had made to the release branch.

With git it’s rather hard to actually lose data and this was no exception. I was able to restore the branch to its previous state by explicitly pushing the commit ref of the branch’s previous state (helpfully given in the output from git push)

git push origin d91922d:release-2012-07-04

Since all my changes had been pushed normally, all I had done was rewind that branch so git was happy for me to add those commits back in (or at least that’s my interpretation – I’m no git wizard.)