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: https://stackoverflow.com/a/27185855/109305

17 Answers 17

First, you create your branch locally:

git checkout -b <branch-name> # Create a new branch and check it out

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 --set-upstream <remote-name> <local-branch-name> 

As described below, the --set-upstream 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.

  • 77
    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
  • 200
    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
  • 84
    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
  • 62
    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
  • 84
    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
  • If git seeems too complicated for you, I recomend you to use Dropbox or Google Drive, REALLY, it's far simpler that git and still have versions/etc... – dseminara Apr 16 '14 at 1:36
up vote 245 down vote accepted

Simple Git 2.0+ solution:

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
  • 3
    I find git push -u origin HEAD as answered here a bit more verbose (you write what you are doing) without being too much to type. Furthermore, a git push -u without additional arguments did not work for me if the branch was created with -t – Qw3ry Sep 25 '17 at 7:37

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>

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.

  • 4
    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
  • 2
    Yeah, you never remember what you last typed as a branch, exactly, so this is the way to go. – marksyzm Feb 23 '16 at 11:34
  • 2
    @marksyzm If you can't remember what branch you're on or what you named it, you probably shouldn't be pushing at all! At least, not without running git status first. – Zenexer Mar 9 '16 at 5:40
  • 1
    Yeah, gotta make sure the world doesn't explode on that push; I agree. – marksyzm Mar 9 '16 at 11:24
  • 1
    This is the most efficient way to create a tracking branch as well as a remote branch at the same time. I'd also like to add git remote show origin as a third step just to visualize the new tracking/tracked relationship. – hb5fa Sep 12 '16 at 14:39

If you wanna actually just create remote branch without having the local one, you can do it like this:

git push origin HEAD:refs/heads/foo

It pushes whatever is your HEAD to branch foo that did not exist on the remote.

  • Doing this completely confused my Visual Studio to the point where it wouldn't start up correctly. The Team Explorer wouldn't load at all, but everything else went bonkers throwing errors also. Just FYI. – Josh Jun 27 '16 at 20:04
  • This sounds like it should work, but when I actually tried it, the our gitlab server didn't recognise the result as a branch. – JosephH Nov 25 '16 at 18:25
  • What branch is foo branched off of on the remote? What if I wanted to foo to branch off of foo2? Is that possible? Thank you. – user674669 Sep 8 '17 at 21:33

If you want to create a branch from the current branch

git checkout -b {your_local_branch_name} 

you want a branch from a remote branch, you can try

git checkout -b {your_local_branch_name} origin/<remote_branch_name>

If you are done with changes you can add the file.

git add -A or git add <each_file_names>

Then do a commit locally

git commit -m 'your commit message'

When you want to push to remote repo

git push -u origin <your_local_branch_name>

All together will be

git checkout -b bug_fixes 

or If you want to create a branch from a remote branch say development

git checkout -b bug_fixes origin/development

You can push to the branch to remote repo by

git push -u origin **bug_fixes**

Anytime you want to update your branch from any other branch say master.

git pull origin master.

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!

  • This is just the help text generated by the command git push when your local branch is not tracked by a remote. – nurettin Jan 4 at 8:08

Create the branch on your local machine and switch in this branch :

$ git checkout -b [name_of_your_new_branch]

Push the branch on github :

$ git push origin [name_of_your_new_branch]

When you want to commit something in your branch, be sure to be in your branch.

You can see all branches created by using :

$ git branch

Which will show :

* approval_messages
  master
  master_clean

Add a new remote for your branch :

$ git remote add [name_of_your_remote] 

Push changes from your commit into your branch :

$ git push origin [name_of_your_remote]

Update your branch when the original branch from official repository has been updated :

$ git fetch [name_of_your_remote]

Then you need to apply to merge changes, if your branch is derivated from develop you need to do :

$ git merge [name_of_your_remote]/develop

Delete a branch on your local filesystem :

$ git branch -d [name_of_your_new_branch]

To force the deletion of local branch on your filesystem :

$ git branch -D [name_of_your_new_branch]

Delete the branch on github :

$ git push origin :[name_of_your_new_branch]

Here All Information

Other Existing project

Easiest Solution... Drumm Roll . git version 2.10.1 (Apple Git-78)

1) git checkout -b localBranchNameThatDoesNotExistInRemote

2) Do your changes , and go a git commit 

3) git push origin localBranchNameThatDoesNotExistInRemote --force

N.B. - The branch you just created in your local environment , and the remote non-existing branch where you are trying to push , must have the same name.

  • 1
    Thanks for your suggestion. Even though you call this an easy solution, I still think git push -u is way easier. Requires that you have one global config line, see stackoverflow.com/a/27185855/109305. I use git push -u constantly, it covers 99% of my usecases when working. – Jesper Rønn-Jensen Mar 18 '17 at 14:24

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.

  • 1
    -u is short for --set-upstream.. so command could be git push -u origin <remote-branch-name> – Uncaught Exception Jul 28 '17 at 10:22
  • fatal: 'origin' does not appear to be a git repository – Dmitry Grinko Jul 23 at 21:03
  • maybe you have to set the origin of your git repository – Maurizio Brioschi Jul 24 at 11:44

Creating a local branch from an existing branch (can be master/ develop/ any-other-branch).

git checkout -b branch_name

Push this to remote

git push -u remote_name local_branch_name:remote_branch_name

Here,

  1. -u : sets the upstream branch
  2. remote_name : git sets the name by default to be "origin" when it creates the repository. This can however be changed to a different arbitrary name.
  3. local_branch_name : is the name of the local branch to be pushed.
  4. remote_branch_name : is the name of the remote branch that we want to be created on remote.

If we remove the local and remote branch names, it will have the format

git push -u remote_name branch_name

This will push the local branch to remote and with the same name as the local branch branch_name. The local branch will be tracking the remote branch as well.

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

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.

How to do through Source Tree

 1: Open SourceTree, click on Repository -> Checkout
 2 :Click on Create New Branch
 3: Select branch where from you want to get code for new branch 
 4: Give your branch name
 5: Push the branch  (by click on Push button)

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.

  • 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:

protected by Community Jul 14 '14 at 19:41

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

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.