Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recover overwritten git commit lost by force push

Tags:

git

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

like image 262
Ali Avatar asked Jun 05 '13 02:06

Ali


People also ask

What is git push -- force?

Force Pushing Git prevents you from overwriting the central repository's history by refusing push requests when they result in a non-fast-forward merge. So, if the remote history has diverged from your history, you need to pull the remote branch and merge it into your local one, then try pushing again.

Can we revert the commit in git?

The git revert command is used for undoing changes to a repository's commit history. Other 'undo' commands like, git checkout and git reset , move the HEAD and branch ref pointers to a specified commit. Git revert also takes a specified commit, however, git revert does not move ref pointers to this commit.


1 Answers

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.

like image 134
Ben Jackson Avatar answered Nov 15 '22 07:11

Ben Jackson