Question

I put my local repository on github. In the github repository, I created README file by clicking the "we recommend a README file link". I wrote a detailed content in it and committed it. But I did not clone this file into my local repository. Recently, I made some changes and pushed them to the github repo. Today I noticed that the README file is missing. I checked my commits and couldn't find anything related to README there.

I am using Eclipse egit plugin to interact with the github repository. I did click on "synchronize workspace" in Eclipse "Team" menu, just to check what it does - this is before I noticed that the file is missing.

Is there anyway to retrieve my README.md file? Please let me know.

My repository: https://github.com/raghavam/d-sparq

Was it helpful?

Solution

From the comments, the original poster says:

Yes, I did tick an option called "force push" in Egit because without that it was giving me an error. What does a force push do when compared to a regular push?

But, I haven't done a fetch or pull - I don't generally do that because I am the only one using that repo and so my current local copy would be the latest.

Yeah, it definitely sounds like you ended up force-pushing your local copy of your repo, which then overwrote the changes you made to your README file on GitHub, especially since you say that you never fetch or pull.

The problem with never fetching or pulling is that you describe editing and committing a README file through GitHub's web interface:

I created README file by clicking the "we recommend a README file link". I wrote a detailed content in it and committed it. But I did not clone this file into my local repository.

So your remote repo has commits that your local repo doesn't. That's probably the error message you were getting when you tried to push, something along these lines, right?:

git push origin master
# To https://github.com/user/repo.git
#  ! [rejected]        master -> master (non-fast-forward)
# error: failed to push some refs to 'https://github.com/user/repo.git'

That message basically means that you need to merge the remote commits with the work in your local repository. But instead of doing that, you just completely overwrote those commits to the README file (that you did remotely) by using force-push, which you should not have done if you didn't really understand what it does.

Be very, very careful with force-push. Only use it when you understand what it does and you're sure that using it is what you want to do, because it's one of the most common ways for Git beginners to end up losing work.

Recovering the README

Since you never fetched or pulled from your repository, the file is basically unrecoverable now, because it was never downloaded to your local remote. You'll need to start over.

OTHER TIPS

It is not unrecoverable, there is a chance it exists on remote repository reflog. Normally you can't access remote repository reflog but github has events API.

# show the events on repo (including commits)
curl -u <username> https://api.github.com/repos/:owner/:repo/events

# create a new branch from the commit sha found in prev command
curl -i -u <username> -X POST -d '{"ref":"refs/heads/new-branch", "sha":"<lost-sha>"}' 
https://api.github.com/repos/:owner/:repo/git/refs

The first command lets you find the sha of lost remote commit. The second command lets you create a branch on that commit. Then simply fetch all changes on local repo and new branch should appear. After that simply merge that branch or cherry-pick the commit and push.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top