Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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.

share|improve this question
    
possible duplicate of How do you make an existing git branch track a remote branch? – markus Jun 3 '12 at 14:40
24  
just to point out --set-upstream is -u – Baiyan Huang Dec 18 '13 at 10:12

10 Answers 10

up vote 3977 down vote accepted

In recent versions of 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 option:

git push -u origin <branch>

Git will set up the tracking information during the push.

share|improve this answer
106  
git push -u was introduced in Git 1.7.0 (2010-02-12). – Chris Johnsen Jun 4 '11 at 4:16
11  
Would you be kind enough to elaborate? Some git commands do more than one thing, and I'm not sure what origin and mynewfeature refer to. Is mynewfeature a branch name? Is origin a shortcut for a full remote repo url? Also what does the -u flag do? – Costa Mar 6 '14 at 21:16
46  
@Costa ‘origin’ is the name of default remote in Git repository. ‘mynewfeature’ here is branch name. -u is short for --set-upstream—for what it does and why it's needed I wouldn't mind some explanation, too. :) – Anton Strogonoff Mar 9 '14 at 6:07
25  
It's also worth noting that if you have an existing tracking branch already set on the branch you're pushing, and push.default is set to upstream, this will not do what you think it will do. It will try to push over the existing tracking branch. Use: git push -u origin mynewfeature:mynewfeature or do git branch --unset-upstream first. – void.pointer May 19 '14 at 18:07
1  
I still needed to 'git branch --set-upstream-to origin/remote' in order for 'git status' to correctly report my branch status with respect to the remote branch. – Paul Whipp Jul 4 '14 at 1:17

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)

share|improve this answer
7  
and git pull --all pulls it all back elsewhere ? kewl – commonpike Oct 19 '14 at 21:15
    
This command sets up tracking to the correct branch without the need to push anything. Thank you. – amey91 Jan 21 '15 at 22:03
4  
Git allows to commit a branch and not push it for very good reasons. Only using git push --all is like dropping a piece of git architecture. If it works for you, it is perfectly ok, great, do it forever. But PLEASE don't recommend others to avoid learning git just because it is a quick way to do things. – Federico Razzoli Jul 20 at 8:24

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
share|improve this answer

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>
share|improve this answer
1  
git branch <branch-name> and git checkout -b <branch-name> both create a branch but checkout switch to the new branch – Robert Aug 11 at 14:37

I suppose that you have already cloned a project like:

git clone http://github.com/myproject.git
  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://example.com/var/git/myapp.git
    git push origin master
    
  3. After that, users should be able to

    git clone http://example.com/var/git/myapp.git
    

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.

ADDED

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
share|improve this answer
    
William's script I linked to does about the same with the additional option to delete remote branches and some safeguards, too – Tobias Kienzler May 4 '10 at 13:07
    
>to push the local branch to remote repo (publish), but make it >trackable so git pull and git push will work immediately. its what github does automatically when you push your code to their repository :-) – VP. May 4 '10 at 13:14
    
This does not respond to the question, the <newbranch> of the original repo is not trackable (and is renamed as <master> is the new repo you clone in step 3). – Lohrun May 4 '10 at 13:16
    
seems kind of overkill. does the git remote add origin make the local branch trackable? is that the key command here? – Roni Yaniv May 4 '10 at 13:21
1  
@Roni Yaniv: no git remote add origin only register a new remote repository. It is just a step needed before pushing your branch to that remote repository (if you don't want to type the whole address each time) – Lohrun May 4 '10 at 13:25

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: https://git-scm.com/docs/git-push 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

share|improve this answer
    
Thank you :) git push -u origin <branch-name> wasn't working for me but using HEAD instead of <branch-name> worked perfectly :) – Daniel Tonon Nov 7 at 7:55

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


Use git publish-branch from [William's miscellaneous Git tools](http://git-wt-commit.rubyforge.org/ (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:

#!/bin/bash
REMOTE=$1 # Rewrite this to make it optional...
BRANCH=$2
# 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...)

share|improve this answer
    
this assumes I have ruby installed. no such luck. any other ideas? – Roni Yaniv May 4 '10 at 13:20
1  
the ruby script calls git push and git config command. I used the code of the script to edit my answer. You might used this information to create a small shell script that does the puslishing for you. – Lohrun May 4 '10 at 13:31
    
William's miscellaneous git tools appears to have moved (that link is now dead). A working link is: gitorious.org/willgit – Mike D Oct 27 '14 at 2:08
    
"William's" link broken again; new link seems to be git-wt-commit.rubyforge.org – ScottJ Aug 11 at 1:07

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>

share|improve this answer

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.

share|improve this answer

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
share|improve this answer
5  
This answer does not add anything to the (plentiful) existing answers and even has some shortcomings (a remote repo does not have to be a public repo). – Dubu Jul 16 '15 at 14:44

protected by Elenasys Jan 14 '14 at 0:20

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.