1

In my remote repository, I only had a readme on which I was working on. I committed the readme after the last update.Afterwards I accidentally pushed a folder from my local repository to that remote one, resulting in losing the readme file I was working on.

Is there any way I can get the readme back?

n.b. went through my command history, noticed that I did a git push origin master --force. I am a beginner, I think there went something wrong while using force. Without force, the error was :

hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

EDIT: the error looks pretty much same like having a merge conflict, but the bone of contention here is that my problem is losing a commit from remote after pushing from local without pulling it. The accepted answer solved my problem.

Proteeti Prova
  • 1,079
  • 4
  • 25
  • 49
  • 1
    Did you "commit the readme after the last update" in the remote repository, or locally? Please give us more details. Did you force-push? – Lasse V. Karlsen Jan 02 '19 at 13:03
  • edited the question, yes used force push – Proteeti Prova Jan 02 '19 at 13:04
  • 1
    Force-push removed the commit from the remote repository. It is still likely on disk there but if this was github, as judging by your comments on the now deleted answer, I'm unsure if you can access the reflog on github directly. I found this - https://medium.com/git-tips/githubs-reflog-a9ff21ff765f - which seems to indicate you can grab the hash of the commit containing that readme file, and that might allow you to bring it back. – Lasse V. Karlsen Jan 02 '19 at 13:05
  • 1
    As for other tips, if, as you say, you're a beginner with git, **do not use force-push**. That and a few other commands can lead to lost commits (not immediately lost permanently, but still). It's an operation similar to "hmm, I can't seem to bend my arm backwards at the angle I want... I know, I'll use a sledgehammer!" – Lasse V. Karlsen Jan 02 '19 at 13:07
  • 1
    You can try accessing the event log from that link I provided above, which should be `https://api.github.com/repos///events`, then see if you can find the commit with your readme and grab the SHA of it. Then, once you have that, see if you can access `https://github.com///commit/`, and see if that gives you the content. – Lasse V. Karlsen Jan 02 '19 at 13:10
  • Possible duplicate of [git: updates were rejected because the remote contains work that you do not have locally](https://stackoverflow.com/questions/24357108/git-updates-were-rejected-because-the-remote-contains-work-that-you-do-not-have) – phd Jan 02 '19 at 13:20
  • https://stackoverflow.com/search?q=%5Bgit%5D+hint%3A+Updates+were+rejected+because+the+remote+contains+work+that+you+do – phd Jan 02 '19 at 13:20
  • @LasseVågsætherKarlsen yes that did solve my problem ! Saved the raw content for recovering my readme. – Proteeti Prova Jan 02 '19 at 13:41

1 Answers1

2

There's a few steps involved so let's go through them.

First and foremost, it's important that you understand that "lost commits" are not permanently lost immediately but may still reside on disk for a limited time. It is very important that you don't push or modify the repository in question further before attempting to fix your current problem

Additionally, depending on what you've done to the repository already, it may already be too late. But let's assume it isn't and see if we can retrieve your lost file.

First, navigate to your GitHub repository in question, let's say this is this (I'm using my own repository as an example, please edit the pertinent bits as indicated below):

https://github.com/lassevk/LVK
                   ^--edit---^

Then let's edit the URL somewhat to grab the event log of your repository:

https://api.github.com/repos/lassevk/LVK/events
                             ^--edit---^

This should give you a json file. In this json file you need to search for the file in question, or its contents, using Ctrl-F in your browser. Let's say you find it and it looks like this:

"commits": [
  {
    "sha": "a52da7697640b77f0b2e16de8d6e8ad8c29924e2",
    "author": {
      "email": "lasse@vkarlsen.no",
      "name": "Lasse Vågsæther Karlsen"
    },
    "message": "Rewrite HttpClient related code.",
    "distinct": false,
    "url": "https://api.github.com/repos/lassevk/LVK/commits/a52da7697640b77f0b2e16de8d6e8ad8c29924e2"

See that SHA there? Copy it and paste it into the following type of link:

https://github.com/lassevk/LVK/commit/a52da7697640b77f0b2e16de8d6e8ad8c29924e2
                   ^-------------------------edit----------------------------^

If this gives you a "404 not found" then you're out of luck as far as I know.

However, if it opens up the commit page then you can open the files modified one by one and download their raw content directly to your local disk as files, these you can then integrate into your repository as you see fit.

If you want to create a branch for the commit and deal with it later, you can by using this URL instead:

https://github.com/lassevk/LVK/tree/a52da7697640b77f0b2e16de8d6e8ad8c29924e2
                   ^--edit---^      ^------------------edit----------------^

This should open up the repository browser for that commit, and you can then use the dropdown for branches to create a new branch that points to the commit. Once a branch points to a commit, it is no longer lost.

For instance, you can then locally do the following:

git fetch
git checkout master                # or whichever branch this is on
git merge NAME-OF-TEMP-BRANCH

This should merge that file into your branch.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825