How do I push a new local branch to a remote Git repository and track it too?

+3096 votes
asked May 4, 2010 by roni-yaniv

I want to be able to do the following:

  1. Create a local branch based on some other (remote or local) branch (via git branch or git checkout -b)

  2. Push the local branch to the remote repository (publish), but make it trackable so git pull and git push will work immediately.

How do I do that?

I know about --set-upstream in Git 1.7, but that is a post-creation action. I want to find a way to make a similar change when pushing the branch to the remote repository.

13 Answers

+109 votes
answered May 4, 2010 by lohrun

Prior to the introduction of git push -u, there was no git push option to obtain what you desire. You had to add new configuration statements.

If you create a new branch using:

$ git checkout -b branchB
$ git push origin branchB:branchB

You can use the git config command to avoid editing directly the .git/config file.

$ git config branch.branchB.remote origin
$ git config branch.branchB.merge refs/heads/branchB

Or you can edit manually the .git/config file to had tracking information to this branch.

[branch "branchB"]
    remote = origin
    merge = refs/heads/branchB
+14 votes
answered May 4, 2010 by tobias-kienzler

edit Outdated, just use git push -u origin $BRANCHNAME

Use git publish-branch from William's miscellaneous Git tools (gitorious repo and clone).

OK, no Ruby, so - ignoring the safeguards! - take the last three lines of the script and create a bash script, git-publish-branch:

REMOTE=$1 # Rewrite this to make it optional...
# Uncomment the following line to create BRANCH locally first
#git checkout -b ${BRANCH}
git push ${ORIGIN} ${BRANCH}:refs/heads/${BRANCH} &&
git config branch.${BRANCH}.remote ${REMOTE} &&
git config branch.${BRANCH}.merge refs/heads/${BRANCH}

Then run git-publish-branch REMOTENAME BRANCHNAME, where REMOTENAME is usually origin (you may modify the script to take origin as default, etc...)

+20 votes
answered May 4, 2010 by vp

I suppose that you have already cloned a project like:

git clone
  1. Then in your local copy, create a new branch and check it out:

    git checkout -b <newbranch>
  2. Supposing that you made a "git bare --init" on your server and created the myapp.git, you should:

    git remote add origin ssh://
    git push origin master
  3. After that, users should be able to

    git clone

NOTE: I'm assuming that you have your server up and running. If it isn't, it won't work. A good how-to is here.


Add a remote branch:

git push origin master:new_feature_name

Check if everything is good (fetch origin and list remote branches):

git fetch origin
git branch -r

Create a local branch and track the remote branch:

git checkout -tb new_feature_name origin/new_feature_name

Update everything:

git pull
+4974 votes
answered Jun 3, 2011 by daniel-ruoso

In Git 1.7.0 and later, you can checkout a new branch:

git checkout -b <branch>

Edit files, add and commit. Then push with the -u (short for --set-upstream) option:

git push -u origin <branch>

Git will set up the tracking information during the push.

+392 votes
answered Jan 20, 2014 by erichbschulz

This will push all your branches to the remote, and --set-upstream tracking correctly for you:

git push --all -u

(Not exactly what the OP was asking for, but this one-liner is pretty popular)

+93 votes
answered Apr 24, 2015 by piyushmandovra

Simply put, to create a new local branch, do:

git branch <branch-name>

To push it to the remote repository, do:

git push -u origin <branch-name>
+12 votes
answered Jun 3, 2015 by cptjack

To create a new branch by branching off from existing branch

git checkout -b <new_branch>

and then push this new branch to repository using

git push -u origin <new_branch>

This creates and pushes all local commits to a newly created remote branch origin/<new_branch>

–6 votes
answered Jun 26, 2015 by shankar-kumar

To upload your local branch of a public repository, you need to cd to the public repository and then use the following code:

git push -u origin branchname
+4 votes
answered Jan 5, 2016 by thuy-trinh

I made an alias so that whenever I create a new branch, it will push and track the remote branch accordingly. I put following chunk into the .bash_profile file:

# Create a new branch, push to origin and track that remote branch
publishBranch() {
  git checkout -b $1
  git push -u origin $1
alias gcb=publishBranch

Usage: just type gcb thuy/do-sth-kool with thuy/do-sth-kool is my new branch name.

+32 votes
answered Jul 5, 2016 by bg17aw

A slight variation of the solutions already given here:

1) Create a local branch based on some other (remote or local) branch:

git checkout -b branchname

2) Push the local branch to the remote repository (publish), but make it trackable so git pull and git push will work immediately

git push -u origin HEAD

Using HEAD is a "handy way to push the current branch to the same name on the remote". Source: In git terms, HEAD (in uppercase) is a reference to the top of the current branch (tree).

The -u option is just short for --set-setupstream. This will add an upstream tracking reference for the current branch. you can verify this by looking in your .git/config file:

enter image description here

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter