Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git - Different Remote for each Branch

I'm unsure of how to ask this properly but I'll try and do my best - I'm by no means a Git aficionado, I know how to use the basic commands but not advanced terminology/functionality.

I have a private repository myrepo cloned from a private server git.mydomain.com. I'm familiar with the process of branching code on the same repository with git checkout -b mybranch - however I'd like to branch to GitHub rather than my private server, resulting in something like this:

Repo       Branch      Remote Location    (Purpose) ------------------------------------------------------------ myrepo --> private --> git.mydomain.com  (Incremental work)   |   +------> public  --> github.com        (Public releases) 

Essentially I'd like to be able to git checkout public and git merge private.

like image 433
Craig Watson Avatar asked Apr 02 '13 21:04

Craig Watson


People also ask

Can you have multiple git remotes?

You can add multiple remotes by using git remote or git config commands or editing the config file. As git can group multiple remotes, you can follow any of the following ways to configure multiple remotes to push simultaneously(no need all). You can set multiple remote URLs to a single remote using git remote.

How do I add a second remote to git?

To add a new remote, use the git remote add command on the terminal, in the directory your repository is stored at. The git remote add command takes two arguments: A unique remote name, for example, “my_awesome_new_remote_repo” A remote URL, which you can find on the Source sub-tab of your Git repo.


1 Answers

You can set a different branch to push to a different server for individual branches by using these commands:

As of Git 1.8.0:

git branch --set-upstream-to origin/foo foo 

Note: If the last foo is left out, it will choose the current branch.

As of Git 1.7.0:

git branch --set-upstream foo origin/foo 

In your case, you would use this by adding your two remotes (mydomain and github) and setting each branch to push to them individually. It might look something like this:

Make sure you add the remotes if you haven't already:

git remote add github git://github.com/foo/myrepo.git git remote add mydomain git://git.mydomain.com/foo/myrepo.git 

Then set the branches to push to the right places:

git branch --set-upstream-to mydomain/private private git branch --set-upstream-to github/public public 

After this is all set up, you can push and pull just by using git push and git pull. This will push and pull to github when you're on the public branch, and to your mydomain.com when you're on your private branch.

like image 65
Jonathan Wren Avatar answered Oct 11 '22 19:10

Jonathan Wren