128

I've taken the following steps so far:

  1. Cloned a remote Git repo
  2. Branched the master branch to an experimental
  3. edited/tested/committed code in the experimental branch

Now, I'm not ready to merge experimental into master. I do however want to push it back to the remote repo as that's the repository I share with a few colleagues. I'd like for them to see what I've done in the experimental branch. I typically just access the remote repo via SSH.

How do I share my local branch on the remote repo, without affecting the remote repo's master branch?

Teun Zengerink
  • 4,277
  • 5
  • 30
  • 32
Coocoo4Cocoa
  • 48,756
  • 50
  • 150
  • 175
  • This link is also very useful in this aspect. [Sharing git branches](http://allmybrain.com/2008/09/15/sharing-git-branches/) – Vishnu Kumar May 31 '11 at 07:59
  • possible duplicate of [How do you make an existing git branch track a remote branch?](http://stackoverflow.com/questions/520650/how-do-you-make-an-existing-git-branch-track-a-remote-branch) – ahsteele Feb 19 '13 at 05:28

5 Answers5

157

According to git push manual page:

 git push origin experimental

Find a ref that matches experimental in the source repository (most likely, it would find refs/heads/experimental), and update the same ref (e.g. refs/heads/experimental) in origin repository with it.
If experimental did not exist remotely, it would be created.

This is the same as:

git push origin experimental:refs/heads/experimental

Create the branch experimental in the origin repository by copying the current experimental branch.
This form is only needed to create a new branch or tag in the remote repository when the local name and the remote name are different; otherwise, the ref name on its own will work.

Or, like mentioned in git tip, you can set up a "Branch’s Default Remote":

You can use git config to assign a default remote to a given branch. This default remote will be used to push that branch unless otherwise specified.

This is already done for you when you use git clone, allowing you to use git push without any arguments to push the local master branch to update the origin repository’s master branch.

git config branch.<name>.remote <remote> 

can be used to specify this manually.


Jan suggests (for git >= 1.7.0) the push -u (or push --set-upstream) option:

For every branch that is up to date or successfully pushed, add upstream (tracking) reference, used by argument-less git-pull(1) and other commands.

That way, you don't have to do any git config.

git push -u origin experimental
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 12
    You should use the `-u` option for `push` to ensure that your local branch tracks the remote one after making it public. If you forget to use the `-u` option, you can just type `git push -u` afterwards in the branch, then `git pull` will work. – Jan Aug 08 '11 at 12:10
  • I have noticted that when you do this `git push origin experimental`, there is no evidence of any packs being uploaded. It looks like *the remote already has all the material from your earlier push; it just has to be hooked in to the refs*. – Kaz Mar 23 '12 at 02:25
  • @Kaz: it would have all the material if those new commits were already pushed in another branch on remote. In that case, `git push origin experimental` would just create the branch `experimental` on the remote. – VonC Mar 23 '12 at 11:32
15

If the name of your branch is experimental, and the name of the remote is origin, then it's

git push origin experimental
John Douthat
  • 40,711
  • 10
  • 69
  • 66
  • Same comment as above, but does that create the experimental branch implicitly on the remote repo, or will it push to remote's master? – Coocoo4Cocoa Apr 30 '09 at 03:35
  • 1
    it pushes the experimental branch and doesn't touch master on either the local side or remotely – John Douthat Apr 30 '09 at 04:45
1

git push -u <remote-name> <branch-name> doesn't work if the newly created branch isn't spawned from the same repo, i.e. if you haven't created the new branch using git checkout -b new_branch, then this will not work.

For eg, I had cloned two different repositories locally and I had to copy repo2/branch1 to repo1/ and then push it too.

This link helped me push my local branch (cloned from another repo) to my remote repo:

brokenfoot
  • 11,083
  • 10
  • 59
  • 80
0

tl;dr

$ git push --set-upstream origin your_new_branch

more info

after you have made few commits into your:

$ git checkout -b your_new_branch
$ git add file
$ git commit -m "changed file"

you push your branch specifying an upstream into one of the remotes repositories like following:

$ git push --set-upstream REMOTE YOUR_BRANCH

remotes can be seen by

$ git remote -v

usually, you will have single default remote origin. so you command would look like:

$ git push --set-upstream origin your_new_branch

and all consequent pushes can be made just with git push.

0

Here is the authoritative github page for github remote management http://github.com/guides/push-a-branch-to-github. It will help you answer all of your questions.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
dylanfm
  • 6,292
  • 5
  • 28
  • 29