7

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
Zeeshan Jan
  • 307
  • 4
  • 8
  • VonC is right, you only think about the remote when pushing to it, or defining the tracking branch to automate the relation. You can push freely to any number of remotes, the tracking branch just sets a default. – scipilot Jun 25 '16 at 09:26

2 Answers2

12

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
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • thanks VonC I have tried that. I created a local branch to track one of the remote branches and when I make changes to that branch and try the command ```git push origin master``` it asks me the credentails for the repo which is correct after I provide the credentials then it just gives the message ```Everything is up-to-date ``` which is very confusing as I am not able to push to the add remote branch. – Zeeshan Jan Jun 25 '16 at 09:42
  • I have just tried with this ```git push origin ``` and it works without specifying the branch name on the remote. – Zeeshan Jan Jun 25 '16 at 09:46
  • @ZeeshanJan did you commit first? And if you did, are you in a detached HEAD? http://stackoverflow.com/questions/3965676/why-did-my-git-repo-enter-a-detached-head-state/3965714#3965714 – VonC Jun 25 '16 at 09:47
  • @ZeeshanJan OK: do a `git branch -avv` to see how your local branches are tied to your mote tracking branches. – VonC Jun 25 '16 at 09:47
  • I have attached the screenshot of the result from the command: http://i.imgur.com/a71ZNuc.png In this screenshot you can see the my two local branches are mapped to 2 remote branches, master & origin_master Is everything looking fine? also I am currently working on the origin_master branch but still the HEAD is mapped to upstram/master branch do I need to move the head to other branch when I am working on different remotes? – Zeeshan Jan Jun 25 '16 at 12:32
  • Also @VonC when I run the command git push origin I see the message: http://i.imgur.com/r7K3rjR.png – Zeeshan Jan Jun 25 '16 at 13:22
  • @ZeeshanJan Is your goal to pull from one remote and push to the other remote? – VonC Jun 25 '16 at 13:31
  • my goal is to be able to pull from the upstream remote repo as that is my seed repo , this pull I will be doing occasionally when there is some changes done on that repo by the open source community on github. 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. – Zeeshan Jan Jun 25 '16 at 15:06
  • Thanks @VonC this is exactly what I needed. Thanks again for assisting me in this. – Zeeshan Jan Jun 26 '16 at 02:58
0

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

git clone -o upstream user@example.com:upstream.git
cd upstream
git branch -m upmaster
git checkout -b master
git remote add origin user@example.com: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
Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167