Zeeshan Jan Zeeshan Jan - 4 months ago 17
Git Question

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.

Answer

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 user@example.com: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
Comments