51

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.

Craig Watson
  • 609
  • 1
  • 5
  • 7
  • Would it fit your workflow to just push that branch to a different origin (Github) when you are ready? – cam Apr 02 '13 at 22:09

2 Answers2

53

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 the github remote when you're on the public branch, and to your mydomain.com remote when you're on your private branch.

Jonathan Wren
  • 3,662
  • 23
  • 29
  • In this same scenario, is it possible to tell git to selectively add files to the public branch? For example if I have folders dist, src and README.md in private and I want only dist and README.md to appear in github i.e. the public repo, how can I go about achieving this? – Saifur Rahman Mohsin Nov 28 '16 at 11:09
  • @SaifurRahmanMohsin The branch will have the same files no matter which server it's pushed to. If you want to have specific files on a specific branch, then commit only those files to that branch. – Jonathan Wren Nov 29 '16 at 02:23
1

First of all add your remotes like

git remote add remote1 https://github.com/username/repo1.git
git remote add remote2 https://github.com/username/repo2.git

Note: remote1 and remote2 are replacement of origin

For those who get error while following Jonathan's above answer like me

error: the requested upstream branch 'remote1/b1' does not exist

The command git branch --set-upstream-to=<remote-name>/<branch-name> expects the remote branch to exist locally or already be fetched from the remote repository.

If the remote branch b1 at remote1 (https://github.com/username/repo1.git) doesn't exist locally, you need to fetch it first.

You can follow these steps to set the upstream branch for b1 and push it to remote1:

Fetch the remote branches from remote1:

git fetch remote1

Create a local branch b1 that tracks the remote branch b1:

git checkout -b b1 remote1/b1

Set the upstream branch for b1 to remote1/b1:

git branch --set-upstream-to=remote1/b1

Push b1 to remote1:

git push remote1 b1

These steps ensure that you have the remote branch b1 locally, set it as the upstream branch, and push it to remote1.

Similarly you can repeat it for remote2 as https://github.com/username/repo2.git for branch 2 b2

Sami
  • 8,168
  • 9
  • 66
  • 99