1

I made some unwanted changes to my forked copy of an upstream repo, and the upstream repo has also changed in that time. So what I want to do is to basically fork a fresh copy of the upstream repo without keeping any of my changes.

Unfortunately I can't seem to figure out at all how to do this. I have tried three methods:

  1. Following the instructions at How do I update a GitHub forked repository? except for the rebase (I don't want my own commits to be replayed). This tells me I am up to date and no changes are made, but my forked repo is still not the same as the current upstream repo.

  2. Following the instructions at Reset local repository branch to be just like remote repository HEAD . This works with the files and changes them, however it also assumes that I am now editing the upstream repo instead. My forked copy does not reset.

  3. Following the instructions at http://www.hpique.com/2013/09/updating-a-fork-directly-from-github/ however I do not get the option of 'switching the base' when I do that.

I'm quite new to GitHub, unfortunately, which makes everything all the more confusing. I suppose as a last resort I could delete my forked repo and start all over again but I would rather not do that if possible.

Edit: Fixed with VonC's help. Needed to clone the upstream repo to a fresh folder as my local copy instead of the existing folder.

Community
  • 1
  • 1
misaochan
  • 890
  • 2
  • 8
  • 25

1 Answers1

3

If you really want to discard your changes, and reset everything to the upstream (original) repo state, you could:

  • git clone the original repo (not your fork)

  • rename the remote origin to upstream

      git remote rename origin upstream
    
  • add origin with the url of your fork:

      git remote add origin https://github.com/<you>/<yourFork>
    
  • push/reset all the branches to your fork:

      git push origin --force --all
    

(see the discussion for an illustration of those commands)

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks @VonC but for some reason it doesn't seem to solve the problem... it says "everything up to date" but when I check my repo on github.com none of the changes went through, it still says "This branch is 7 commits ahead of ". I think I did everything as per instructions except for the fact that I already had a remote upstream so I had to remove it. I will attach a screenshot of my command line logs so you can see if I'm doing anything wrong, if you don't mind. – misaochan Oct 14 '15 at 10:33
  • @melange right: that is because the branch configuration still says: `branch.xxx.remote upstream`. Replace `upstream` by `origin` in your `.git/config` for every `branch.xxx.remote` line, and try the `git push --force --all` again. – VonC Oct 14 '15 at 10:38
  • VonC, you mean edit the upstream URL in the config file in my .git directory, right? – misaochan Oct 14 '15 at 10:41
  • @melange no: that was done by the git remote commands above. I mean changing the word `upstream` by `origin` in the lines of the `.git/config` file with `branch.xxx.remote` (like `branch.master.remote upstream` changed into `branch.master.remote origin`) – VonC Oct 14 '15 at 10:43
  • Do you mean lines like this in the .git/config? `fetch = +refs/heads/*:refs/remotes/upstream/*` I have replaced that and rerun the git push but it seems to produce the same result. I'll attach another screenshot... Sorry about that, and thanks for the patience! – misaochan Oct 14 '15 at 10:48
  • @melange no the lines which include `branch..remote` – VonC Oct 14 '15 at 10:51
  • There is no such line in my .git/config file though. :( I have pasted the entire config file in my opening post. – misaochan Oct 14 '15 at 10:52
  • @melange I just saw your config file, And I don't understand why upstream and origin seem to have the same url. The git remote commands I mentioned should have set them to two different url – VonC Oct 14 '15 at 10:52
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/92246/discussion-between-vonc-and-melange). – VonC Oct 14 '15 at 10:53
  • @melange You cloned the repo and then ran the command without cd-ing to the repo folder. The rest of the commands should be run from the project root – Harish Ved Oct 14 '15 at 11:14
  • @VonC I think the local folder he is cloning to also has a parent .git repo – Harish Ved Oct 14 '15 at 11:16
  • @melange can you join http://chat.stackoverflow.com/rooms/92246/discussion-between-vonc-and-melange ? – VonC Oct 14 '15 at 11:21
  • @HarishVed can you join http://chat.stackoverflow.com/rooms/92246/discussion-between-vonc-and-melange? – VonC Oct 14 '15 at 11:21