Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 48 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save ElectricRCAircraftGuy/8ca9c04924ac11a50d48c2061d28b090 to your computer and use it in GitHub Desktop.
Save ElectricRCAircraftGuy/8ca9c04924ac11a50d48c2061d28b090 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. Manually fork their repo via the Github website directly.

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

    git remote rename origin upstream
  4. Add a new origin which now points to your fork you just made above (instead of to to the original repository).

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

    git fetch origin
  6. Make local branch "master" track remote branch "origin/master" (ie: remote branch "master" from remote "origin" which you just set above). See more syntax examples here: https://stackoverflow.com/a/2286030/4561887.

    git branch --set-upstream master origin/master (deprecated)

    git branch --set-upstream-to origin/master master
  7. Push to your fork via your "origin" remote (the word origin should be able to be omitted (ie: just write git push) if you did Step 4).

    git push origin
@ThingEngineer
Copy link

Perfect, thank you!

@azamk100
Copy link

Thank you! Worked great

@MicaelPereira
Copy link

Thank you!
That's work.

@qzzhang
Copy link

qzzhang commented Mar 1, 2018

Super helpful, thank you!

@kxrob
Copy link

kxrob commented Jul 14, 2020

More simple & robust: After re-sync (fetch/pull..) and fork on githup.com simply change the origin (=default) URL to that fork:

git remote set-url origin https://github.com/forkuser/forkedproject.git
git fetch

Optionally add the original remote repo too - as "upstream" or whatever :

git remote add upstream https://github.com/origuser/origproject.git

@TomasHubelbauer
Copy link

TomasHubelbauer commented Jan 20, 2023

⚠️ Easier solution alert ⚠️

  • Download and install https://cli.github.com
  • Run gh repo fork in the repository you cloned that isn't yours
  • Allow the tool to update origin
  • Make your changes and push
  • Select origin as your desired upstream, upstream is a new origin pointing to the original fork's repo!

https://cli.github.com/manual/gh_repo_fork

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment