Ali
Ali

Reputation: 10453

Recover overwritten git commit lost by force push

I was writing a README.md file in the remote server and completed that one, but I accidentally did a force push to the remote by using git push origin master and now the README.md is gone

Is there a way I can retrieve that commit? I have written a very long README.md file and really want to see if I can get it back :(

I was looking though the git fsck and couldn't find the one about the README.md that I have commit from the remote server on github.com

Upvotes: 10

Views: 6506

Answers (2)

pktangyue
pktangyue

Reputation: 8524

In Public Activity of github, you may see something like this:

XXX pushed to master at XX/XXX 
d506bb1 Update README.md. 

And here d506bb1 is your lost commit.

Upvotes: 8

Ben Jackson
Ben Jackson

Reputation: 93690

If it existed in some working copy, go to that repo and use git reflog to find a revision that contained it and git checkout to go to that revision (outside of any branch). Then you can copy the file out wherever you want.

If the server is bare it is probably still an object on the server but no kind of git fetch will copy it to any other repo because there are no references to it. In that case you'll need access to the bare repo to run your git fsck directly.

Upvotes: 11

Related Questions