2

I have forked repo on github. I synced my forked repo with the production one creating an upstream endpoint so that the command git remote -v

origin  git@github.com:some_repo.git (fetch)
origin  git@github.com:some_repo.git (push)
upstream    git@github.com:some_repo.git (fetch)
upstream    git@github.com:some_repo.git (push)

To sync, I ran this

git fetch upstream;git checkout master;git merge upstream/master

But the merge failed with tons of conflicts. So I decided to force the merge and ran

git push --force upstream master:master

thinking this should force from the upstream master branch into my local master branch.

The problem is that I misled and it seems that state of my forked repo have been pushed on the remote upstream branch so that I lost all the commits on the upstream remote branch, this one being formerly ahead from the actual state of my forked repo. How can I recover the actual state of the upstream remote branch ?

epsilones
  • 11,279
  • 21
  • 61
  • 85

1 Answers1

4

What you just did was to replace the remote repo history by your local history, while you meant to do the opposite.

What you should have done was:

git reset --hard upstream/master

But since upstream/master is gone, check if you can see it in your local git reflog.

If not, I mentioned before that GitHub keep track of push event in "Does github remember commit IDs?": By curling the https://api.github.com/repos/<user>/<repo>/events url, and looking for push events, you can find the commit pushed to master before your own.

Once you have that commit, you can reset your local branch to it, and push again.

git reset --hard old_sha1
git push --force upstream master:master
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • thank you for your response : in fact, it is weird but I simply proceeded to a `git add .` and `git commit...` and then I recovered all the 9 new commits. But two things are quite weird : 1) if I run `git reset --hard HEAD^`, I go backward of 9 commits. These 9 commits seem to correspond to 1 single commit. Second, after working, deleting some files, add some new work and commiting, I tried to squash some of them, then I get a message `warning: Cannot merge binary files:... - error: could not apply SHA1_commit...` where the binary files are precisely the one I deleted. Do you have an idea ? – epsilones May 06 '17 at 12:39
  • @Newben it could be a merge commit (the one you did on your first pull) – VonC May 06 '17 at 12:43
  • @Newben If you can, check the reflog and reset your branch to the sha1 of upstream/master – VonC May 06 '17 at 12:43
  • 1
    I tried to curl this `https://api.github.com/repos///events` and I found `message: "Not Found"`. Btw, how can I use `git reflog` to check the branch `upstream/master` status – epsilones May 06 '17 at 13:45
  • @Newben You did replace user and repo by the url of your upstream repo, right? – VonC May 06 '17 at 13:47
  • @Newben "how can I use git reflog to check the branch upstream/master status": by typing `git reflog` in your local repo: see https://git-scm.com/docs/git-reflog – VonC May 06 '17 at 13:48
  • @Newben If your repository is private you need to curl Github's API like this `curl -u https://api.github.com/repos///events`. The parameter `-u ` will let you authenticate and view the events – toioski Dec 03 '17 at 16:14