Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to move to a fork after cloning

If you are like me you find yourself cloning a repo, making some proposed changes and then deciding to later contributing back using the GitHub Flow convention. Below is a set of instructions I've developed for myself on how to deal with this scenario and an explanation of why it matters based on jagregory's gist.

To follow GitHub flow you should really have created a fork initially as a public representation of the forked repository and the clone that instead. My understanding is that the typical setup would have your local repository pointing to your fork as origin and the original forked repository as upstream so that you can use these keywords in other git commands.

  1. Clone some repo (you've probably already done this step)

    git clone git@github...some-repo.git
  2. Fork their repo on Github

  3. In your local, rename your origin remote to upstream

    git remote rename origin upstream
  4. Add a new origin

    git remote add origin git@github...my-fork
  5. Fetch from new origin

    git fetch origin
  6. Set origin master

    git branch --set-upstream master origin/master

    git branch --set-upstream-to origin/master master
  7. Push to fork (origin should be able to omitted given step 4)

    git push origin
@jpierson

This comment has been minimized.

Copy link
Owner Author

jpierson commented Mar 15, 2017

Using git branch --set-upstream master origin/master from step 5 today didn't work as it seemed to for me originally. This time I recieved the following error.

The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to
error: the requested upstream branch 'origin/master' does not exist
hint:
hint: If you are planning on basing your work on an upstream
hint: branch that already exists at the remote, you may need to
hint: run "git fetch" to retrieve it.
hint:
hint: If you are planning to push out a new local branch that
hint: will track its remote counterpart, you may want to use
hint: "git push -u" to set the upstream config as you push.

Maybe I did something different. As an alternative I can explicitly state the remote when pushing my changes.

@ElectricRCAircraftGuy

This comment has been minimized.

Copy link

ElectricRCAircraftGuy commented Jan 12, 2018

I added a bunch of clarity, especially to #6, in my fork of your gist here: https://gist.github.com/ElectricRCAircraftGuy/8ca9c04924ac11a50d48c2061d28b090.

Feel free to pull my gist back into yours if you like, since gists don't allow pull requests.

@bvacaliuc

This comment has been minimized.

Copy link

bvacaliuc commented Aug 19, 2018

Thanks for your work!

In my case, I had already made a branch on my upstream clone before I had decided to fork. So my step 6, (6b) and 7 had to be a little different, based on https://stackoverflow.com/questions/11619593/how-to-change-the-fork-that-a-repository-is-linked-to:

git branch --set-upstream-to origin/$branch $branch
git remote set-url origin --push git@github...my-fork
git push --set-upstream origin $branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment