Zeeshan Jan
Zeeshan Jan

Reputation: 307

Working with multiple remotes in git

I am working on a new project and would like to start with a seed project which I have found on GitHub.
I have cloned the seed project locally, it now shows 1 remote branch when I execute the command:

git remote -v

However I would like to add a new remote to this repo and make all my changes or scale the source code on this new repo which is a private repo.

After adding new remote now I am able to see 2 remotes in the repo.

How can switch between the 2 remotes?
I don't think commands like git checkout will work when working on 2 branches from 2 different remotes.

Upvotes: 7

Views: 8980

Answers (2)

Jeff Puckett
Jeff Puckett

Reputation: 40861

I like to set up another branch dedicated to tracking the starter project.

git clone -o upstream [email protected]:upstream.git
cd upstream
git branch -m upmaster
git checkout -b master
git remote add origin [email protected]:myrepo.git
git push -u origin master

Now you work normally from master pushing and pulling to your repo.

When you want to pull upstream changes, grab them to your dedicated branch and rebase or merge into your branch.

git checkout upmaster
git pull
git checkout master
git rebase upmaster

Upvotes: 0

VonC
VonC

Reputation: 1323633

how can switch between the 2 remotes

You don't exactly "switch", you simply mention the name of the remote you want to use:

git push origin
# or
git push remote2

That way, you can pull from one remote and push to another.

You could even use only one remote (the default origin one), and set a different push url:

git remote set-url --push origin [email protected]:repo.git

I don't think commands like git checkout will work when working on 2 branches from 2 different remotes.

git checkout is more for local branches.
You can create a local branch based on a remote tracking branch:

git checkout -b abranch remote2/abranch

While the other remote which is my origin is my private remote and I will be mainly working on that remote and doing pull and push.
On the upstream remote I will only be doing pull.

That is the definition of the triangular workflows:

enter image description here

You clone from origin as usual, but fetch form upstream.

$ git remote add upstream https://github.com/atom/atom
$ git fetch upstream

Create local branches based on origin/abranch, but don't forget to rebase that branch on top of upstream/abranch, whenever a fetch brings new commits from upstream.

git checkout -b abranch origin/abranch
git rebase upstream/abranch
git push --force

Upvotes: 12

Related Questions