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
@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