Skip to content

Instantly share code, notes, and snippets.

@jpierson
Forked from jagregory/gist:710671
Last active December 26, 2022 21:48
Show Gist options
  • Save jpierson/b6c0815e9dd7078f6b8cc3cb9076ddf4 to your computer and use it in GitHub Desktop.
Save jpierson/b6c0815e9dd7078f6b8cc3cb9076ddf4 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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
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
Copy link

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