Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to move to a fork after cloning
So you've cloned somebody's repo from github, but now you want to fork it and contribute back. Never fear!
Technically, when you fork "origin" should be your fork and "upstream" should be the project you forked; however, if you're willing to break this convention then it's easy.
* Off the top of my head *
1. Fork their repo on Github
2. In your local, add a new remote to your fork; then fetch it, and push your changes up to it
git remote add my-fork git@github...my-fork.git
git fetch my-fork
git push my-fork
Otherwise, if you want to follow convention:
1. Fork their repo on Github
2. In your local, rename your origin remote to upstream
git remote rename origin upstream
3. Add a new origin
git remote add origin git@github...my-fork
4. Fetch & push
git fetch origin
git push origin
@marcstober

This comment has been minimized.

Copy link

marcstober commented May 20, 2011

Thanks!

@hablutzel1

This comment has been minimized.

Copy link

hablutzel1 commented Apr 2, 2012

Thank you very much, only one thing, now I want to make my repo the master branch because in the configuration I can see something like:

[branch "master"]
remote = upstream
merge = refs/heads/master

@vishalsuvagia

This comment has been minimized.

Copy link

vishalsuvagia commented Oct 14, 2014

Thanks a lot , James.

@YA2O

This comment has been minimized.

Copy link

YA2O commented Aug 7, 2015

Thank you so much 👍

@digitalfinesse

This comment has been minimized.

Copy link

digitalfinesse commented Aug 9, 2015

Thank you very much!!!!!!!

@stremlenye

This comment has been minimized.

Copy link

stremlenye commented Sep 14, 2015

👍

@yuriy-skalskyy-lebara

This comment has been minimized.

Copy link

yuriy-skalskyy-lebara commented Sep 23, 2015

Great!

@jozaco

This comment has been minimized.

Copy link

jozaco commented Oct 28, 2015

Thank you very much!!

@enriched

This comment has been minimized.

Copy link

enriched commented Oct 30, 2015

👍

@paulmaunders

This comment has been minimized.

Copy link

paulmaunders commented Nov 9, 2015

👍 worked for me too!

@nighto

This comment has been minimized.

Copy link

nighto commented Nov 18, 2015

👍

@snazzware

This comment has been minimized.

Copy link

snazzware commented Mar 9, 2016

👍

@gtitievsky

This comment has been minimized.

Copy link

gtitievsky commented May 16, 2016

thanks!

@HeatfanJohn

This comment has been minimized.

Copy link

HeatfanJohn commented Jul 5, 2016

I followed your "convention" instructions and they worked!

However, to allow me to just perform git push to push updates to my fork, I had to perform the following command:

git push -u origin branchname

Without performing the push -u a simple git push tried to push to the upstream repo.

@Hi-Angel

This comment has been minimized.

Copy link

Hi-Angel commented Aug 29, 2016

I'd wanted to give you a thumb up, but github doesn't have them on "gist"'s for some reason. So, to show that in 2016 the "gist" is still useful: thanks!

@shanx

This comment has been minimized.

Copy link

shanx commented Sep 21, 2016

Watch out with the second convetion! Push will by default now push to upstream!

@mdaizovi

This comment has been minimized.

Copy link

mdaizovi commented Sep 22, 2016

This is super helpful, thank you so much. I wanted to add something about an issue I was having in my particular case. I kept getting:

Error: Permission denied (publickey)

because I was using 2 github accounts, my real one and a general one I made so I could pass on the project to someone else so I can keep the original and they can maintain the fork. My issue was related to my secondary account not having proper SSH bla blah.

What worked was swapping out the:
git remote add origin git@github...my-fork
with:
git remote add upstream https://github.com/...my-fork
and using the https address instead

After I suspected this was the problem I had to remove the fork repository and start over, replacing the git@github with https, and then it worked perfectly.

Thanks!

@Zastai

This comment has been minimized.

Copy link

Zastai commented Oct 23, 2016

The second (and IMHO preferred) form needs one more command, to switch your master branch from upstream to origin:

git checkout -B master --track origin/master

@IBwWG

This comment has been minimized.

Copy link

IBwWG commented Jan 5, 2017

Isn't the simplest way to switch to your fork being the new remote origin just like this?

git remote set-url origin https://github.com/myusername/myrepo.git

(After that, git push pushes to your fork. Done.)

@garrettgsb

This comment has been minimized.

Copy link

garrettgsb commented Jan 24, 2017

Awesome-- Saved my life.

@SHaTRO

This comment has been minimized.

Copy link

SHaTRO commented Jan 28, 2017

Wrote a bash script to do this for 1.8.0 and better (only tested it on 1.9.1, though).
The gist is git-forked-to

Thank you so much!

@dsummersl

This comment has been minimized.

Copy link

dsummersl commented Jan 28, 2017

If you use github's hub, you can just do git fork and it'll fork your current github project, and add your new fork as a remote with your username. For the ultimate win you can change 'origin' to be your fork, and change the project your forked to upstream with some renames. To recap:

# fork the project you cloned, creates a new remote with your username (and forks the project on github if you haven't already done so).
git fork
git remote rename origin upstream
git remote rename YOURUSERNAME origin
@Tzaphkiel

This comment has been minimized.

Copy link

Tzaphkiel commented Feb 10, 2017

'Convention' part worked like a charm, thanks !

@seth10

This comment has been minimized.

Copy link

seth10 commented Mar 6, 2017

Perfect!

@gusrsilva

This comment has been minimized.

Copy link

gusrsilva commented Mar 17, 2017

Works perfectly thank you!

@pashute

This comment has been minimized.

Copy link

pashute commented Mar 23, 2017

Can I do this directly in Eclipse Neon? If so, how?
Or if not, can I do that from GitHub Desktop? and if so, how?
And is there an image or animation somewhere that explains this "fetch" and "upstream" and the rest of all this buzz?
If not, and someone points me to a good explanation in words, I'll be happy to make an animation...

@krobinson

This comment has been minimized.

Copy link

krobinson commented Apr 15, 2017

Worked wonderfully thank you.

@acosonic

This comment has been minimized.

Copy link

acosonic commented May 23, 2017

@IBwWG best way, worked!

@cells2numbers

This comment has been minimized.

Copy link

cells2numbers commented Jun 6, 2017

Very helpful, thx

@kendopunk

This comment has been minimized.

Copy link

kendopunk commented Jul 19, 2017

Much obliged...clean and simple.

@cmndrsp0ck

This comment has been minimized.

Copy link

cmndrsp0ck commented Jul 25, 2017

Thanks for this one.

@JosephKJ

This comment has been minimized.

Copy link

JosephKJ commented Sep 6, 2017

You made my day! Thank you!

@ProbonoBonobo

This comment has been minimized.

Copy link

ProbonoBonobo commented Sep 9, 2017

Thank you!

@bdamm

This comment has been minimized.

Copy link

bdamm commented Sep 25, 2017

Groovy and worked nice and easy! Thanks

@doxman

This comment has been minimized.

Copy link

doxman commented Oct 1, 2017

👍

@adgao

This comment has been minimized.

Copy link

adgao commented Dec 15, 2017

works perfectly!

@ElectricRCAircraftGuy

This comment has been minimized.

Copy link

ElectricRCAircraftGuy commented Jan 12, 2018

Thanks! 2 forks down from yours, here's my version: https://gist.github.com/ElectricRCAircraftGuy/8ca9c04924ac11a50d48c2061d28b090

@Nakilon

This comment has been minimized.

Copy link

Nakilon commented Jan 13, 2018

@IBwWG 's command worked for me.

@d4hines

This comment has been minimized.

Copy link

d4hines commented Feb 12, 2018

Thanks a bunch for this!

@lucidyan

This comment has been minimized.

Copy link

lucidyan commented Feb 18, 2018

@ElectricRCAircraftGuy Very nice, thanks a lot!

@RobLui

This comment has been minimized.

Copy link

RobLui commented Feb 24, 2018

Awesome, T Y :)

@yeemey

This comment has been minimized.

Copy link

yeemey commented Apr 13, 2018

Thank you!

@JiniousChoi

This comment has been minimized.

Copy link

JiniousChoi commented Jul 5, 2018

A question for you:

$ git remote -v
origin	https://github.com/JiniousChoi/python-promise-tutorial (fetch)
origin	https://github.com/JiniousChoi/python-promise-tutorial (push)

I forked a repository of someone's and then I cloned it on my local machine.
According to your tutorial here, I expected to see upstream which points to the original repository of the other guy's when I issued this command git remove -v, but I only see my forked repository.

Isn't it that github manages the information whether it's forked or not somewhere in their server?
I assumed that forking a repository is a added feature by github on top of git.
Let's assume that I pose a random set of cloned repositories on my machine, and can any of your tell which ones are forked and which ones are original of my own, only using git commands?

@vikkrantxx7

This comment has been minimized.

Copy link

vikkrantxx7 commented Aug 2, 2018

I cloned a repo then created a repo on my GitHub then pushed the cloned repo to my GitHub repo. Now how can I compare the original github repo with my github repo as it is not forked technically as it is normally done after forking?

@rkonno

This comment has been minimized.

Copy link

rkonno commented Sep 12, 2018

Thanks!

@huuchi207

This comment has been minimized.

Copy link

huuchi207 commented Oct 11, 2018

Isn't the simplest way to switch to your fork being the new remote origin just like this?

git remote set-url origin https://github.com/myusername/myrepo.git

(After that, git push pushes to your fork. Done.)

simpler way. thank you very much :D

@kpanchen

This comment has been minimized.

Copy link

kpanchen commented Jan 20, 2019

Thanks a lot!! Put me on the right path!

@BoxOfCereal

This comment has been minimized.

Copy link

BoxOfCereal commented Jan 21, 2019

Thank you for the gist! And thank you IBwWG for set-url command!

@Stuart-N

This comment has been minimized.

Copy link

Stuart-N commented Jan 23, 2019

Thanks!

@Justin2997

This comment has been minimized.

Copy link

Justin2997 commented Feb 1, 2019

Thanks !

@sslupsky

This comment has been minimized.

Copy link

sslupsky commented May 8, 2019

Thank you. :-)

@DanielRiezebos

This comment has been minimized.

Copy link

DanielRiezebos commented May 24, 2019

Thank you very much!

@alex-che

This comment has been minimized.

Copy link

alex-che commented Aug 28, 2019

Isn't the simplest way to switch to your fork being the new remote origin just like this?

git remote set-url origin https://github.com/myusername/myrepo.git

(After that, git push pushes to your fork. Done.)

This.

@ViralTaco

This comment has been minimized.

Copy link

ViralTaco commented Nov 20, 2019

You legend <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.