Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I created a local branch which I want to 'push' upstream. There is a similar question here on Stack Overflow on how to track a newly created remote branch.

However, my workflow is slightly different. First I want to create a local branch, and I will only push it upstream when I'm satisfied and want to share my branch.

  • How would I do that? (my google searches did not seem to come up with anything).
  • How would I tell my colleagues to pull it from the upstream repository?

UPDATE With Git 2.0 there is a simpler answer I have written below: http://stackoverflow.com/a/27185855/109305

share|improve this question
4  
did anyone ever answer you second question? >>And how would I tell my colleagues to pull it from the upstream repository? –  milkplus Oct 27 '10 at 22:14
37  
@milkplus, if you have a cloned repository already, all you need to do is git checkout origin/<branch-name> -b <branch-name> which will create a new local branch called <branch-name which tracks the remote branch of the same name and checks it out for you. –  Brett Ryan Dec 6 '10 at 4:21
    
Possibly related: Pro Git: 3.5 Git Branching - Remote Branches. –  Cupcake May 5 '14 at 4:19

11 Answers 11

up vote 1754 down vote accepted

First, you create your branch locally:

git checkout -b your_branch

The remote branch is automatically created when you push it to the remote server. So when you feel ready for it, you can just do:

git push <remote-name> <branch-name>

Where <remote-name> is typically origin, the name which git gives to the remote you cloned from. Your colleagues would then just pull that branch, and it's automatically created locally.

Note however that formally, the format is:

git push <remote-name> <local-branch-name>:<remote-branch-name>

But when you omit one, it assumes both branch names are the same. Having said this, as a word of caution, do not make the critical mistake of specifying only :<remote-branch-name> (with the colon), or the remote branch will be deleted!

So that a subsequent git pull will know what to do, you might instead want to use:

git push -u <remote-name> <local-branch-name>

As described below, the -u option sets up an upstream branch:

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.

share|improve this answer
58  
Note that default behavior of git is to push matching refs, so git push <remote> would not push branch if it is not present on <remote>. –  Jakub Narębski Oct 5 '09 at 21:55
144  
You might want to use git push -u <remote-name> <branch-name> instead, so that a subsequent git pull will know what to do. –  Bart Schuller Apr 6 '11 at 15:03
72  
Instead of explicitly specifying the server name, you can just use origin, which means "the server I got the rest of this repo from": thus git push origin <branch-name>. –  jpatokal May 16 '11 at 6:31
44  
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 Jul 28 '11 at 13:07
48  
Putting it all together, git push -u origin <local-branch-name> is what worked for me. –  Samo Jun 15 '12 at 19:14

First, you must create your branch locally

git checkout -b your_branch

After that, you can work locally in your branch, when you are ready to share the branch, push it. The next command push the branch to the remote repository origin and tracks it

git push -u origin your_branch

Teammates can reach your branch, by doing:

git fetch
git checkout origin/your_branch

You can continue working in the branch and pushing whenever you want without passing arguments to git push (argumentless git push will push the master to remote master, your_branch local to remote your_branch, etc...)

git push

Teammates can push to your branch by doing commits and then push explicitly

... work ...
git commit
... work ...
git commit
git push origin HEAD:refs/heads/your_branch

Or tracking the branch to avoid the arguments to git push

git checkout --track -b your_branch origin/your_branch
... work ...
git commit
... work ...
git commit
git push
share|improve this answer
16  
thank you for pointing out that first you need to create the branch locally –  Naoise Golden Apr 13 '12 at 16:53
7  
Thanks for this great answer. Recent versions of git setup tracking automatically with git checkout your_branch –  kbrock Oct 6 '12 at 12:35
1  
You can use git push origin HEAD to use the same branch name. –  Zenexer Feb 24 '14 at 15:03
4  
And this is why people hate using git. Why are simple things so complicated to do?? –  Eddie Sullivan Feb 26 '14 at 16:40
    
Because git was not designed; it was written. Commands were tacked on as needed without thought for the API except "someone will probably write a 'porcelain' that makes this all make sense someday". –  AlexChaffee Apr 15 '14 at 0:12

As stated in the previous answers,

git push <remote-name> <local-branch-name>:<remote-branch-name>

is enough for pushing a local branch.

Your colleagues, can pull all remote branches (including new ones) with this command:

git remote update

Then, to make changes on the branch, the usual flow:

git checkout -b <local-branch-name> <remote-name>/<remote-branch-name>
share|improve this answer
    
@Lucian, let's not forget the -u arg for push and the -t arg for checkout to once set up tracking which will make pull automatic for the local branch. –  A-B-B Mar 31 '14 at 23:43

As of Git 2.0 the behaviour has become simpler:

You can configure git with push.default = current to make life easier:

I added this so now I can just push a new branch upstream with

$ git push -u

-u will track remote branch of same name. No with this configuration you will auto-guess the remote reference to git push. From git.config documentation:

push.default

Defines the action git push should take if no refspec is explicitly given.

push.default = current - push the current branch to update a branch with the same name on the receiving end. Works in both central and non-central workflows.

For me, this is a good simplification of my day-to-day Git workflow. The configuration setting takes care of the 'usual' use case where you add a branch locally and want to create it remotely. Also, I can just as easily create local branches from remotes by just doing git co remote_branch_name (as opposed to using --set-upstream-to flag).

I know this question and the accepted answers are rather old, but the behaviour has changed so that now configuration options exists to make your workflow simpler.

To add to your global Git configuration, run this on the command line:

$ git config --global push.default current
share|improve this answer
1  
Indeed, this is much simpler. –  experquisite Dec 30 '14 at 4:25
    
Thanks @experquisite :) –  Jesper Rønn-Jensen Jan 6 at 19:34
    
Definitely simper, but is it a little dangerous because it could push on to master? –  David Mann Feb 13 at 20:06
1  
@DavidMann it will push whichever branch you are on. Typically I find this no problem, since I create a branch locally, and then push it. I am already on that branch. This is by far the 90% usecase of pushing a local branch to the remote for me. –  Jesper Rønn-Jensen Apr 22 at 8:28

Create a new branch locally based on the current branch:

git checkout -b newbranch

Commit any changes as you normally would. Then, push it upstream:

git push -u origin HEAD

This is a shortcut to push the current branch to a branch of the same name on origin and track it so that you don't need to specify origin HEAD in the future.

share|improve this answer
    
This helped in my case: git push -u origin HEAD. I think it's the most clear way. –  Scadge Mar 7 '14 at 12:12

Now with git, you can just type, when you are in the correct branch

git push --set-upstream origin <remote-branch-name>

and git create for you the origin branch.

share|improve this answer

I know this question is well answered, but just wanted to list the steps I take to create a new branch "myNewBranch" and push to remote ("origin" in my case) and set up tracking. Consider this the "TL;DR" version :)

# create new branch and checkout that branch
git checkout -b myNewBranch
# now push branch to remote 
git push origin myNewBranch
# set up the new branch to track remote branch from origin
git branch --set-upstream-to=origin/myNewBranch myNewBranch
share|improve this answer

Here is how you do it in eclipse through Egit.

1) Go the "Git Repository Exploring" view and expland the git project to which you want to create a branch. Under Brances -> Local .. select the branch for which you want to create the branch ( In my case I selected master .. you can select another other branch if you wish) .. then right click and click on Create Branch option .. and select the checkout this project option and then click the finish button.

2) Now from the project explorer select the project .. right click then Team -> Push Branch.

A new remote branch will be created. You can give the name of the branch to your colleagues so that they can pull it.

share|improve this answer
    
Tangential warning about Egit -- and all JGit-based clients, AFAIK: they don't support .gitattributes! This means if your team uses a mix of Windows(CRLF) and Linux/OSX(LF) you must depend on each client having the right settings at all times. Naturally it's better to manage line endings centrally at the repo or project level, and .gitattributes is the supported way to do this. So, if you don't absolutely have to use Egit... don't! :) –  cweekly May 19 '14 at 21:46

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:

share|improve this answer

Just wanted to add that while:

git checkout -b {branchName}

Creates a new branch, it also checks out that branch / makes it your current branch. If, for some reason, all you want to do is snap off a branch but not make it your current branch, then you would use the following command:

git branch {branchName}

In the first command, "checkout" makes said branch your current branch, and the "-b" means: this branch doesn't exist yet, so make it for me.

share|improve this answer

First you create the branch locally:

git checkout -b your_branch

And then to create the branch remotely:

git push --set-upstream origin your_branch

Note: This works on the latests versions of git:

$ git --version
git version 2.3.0

Cheers!

share|improve this answer

protected by Community Jul 14 '14 at 19:41

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.