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 know how to make a new branch that tracks remote branches, but how do I make an existing branch track a remote branch?

I know I can just edit the .git/config file, but it seems there should be an easier way.

share|improve this question
3  
As noted below, for an existing branch, you can use git push -u origin branch-name. –  Zags Mar 6 '14 at 23:46
1  
For reference, the git branch documentation‌​. –  Cupcake May 23 '14 at 18:26

11 Answers 11

up vote 2105 down vote accepted

Given a branch foo and a remote upstream:

As of Git 1.8.0:

git branch -u upstream/foo

Or, if local branch foo is not the current branch:

git branch -u upstream/foo foo

Or, if you like to type longer commands, these are equivalent to the above two:

git branch --set-upstream-to=upstream/foo

git branch --set-upstream-to=upstream/foo foo

As of Git 1.7.0:

git branch --set-upstream foo upstream/foo

Notes:

All of the above commands will cause local branch foo to track remote branch foo from remote upstream. The old (1.7.x) syntax is deprecated in favor of the new (1.8+) syntax. The new syntax is intended to be more intuitive and easier to remember.


See also: Git: Why do I need to do `--set-upstream` all the time?

share|improve this answer
51  
Is "upstream" the name of the remote? i.e. what most would call "origin" by default? –  Andrew Vit Jun 26 '10 at 6:30
97  
@Andrew: Yes. git branch --set-upstream master origin/master would be equivalent to what is automatically done when you initially clone a repository. –  Dan Moulding Jun 26 '10 at 10:09
49  
On a related note, adding this to your gitconfig is awesome: "[push] default=tracking" this will make it so that pushes will go the same place that pulls come from :) –  orange80 Dec 5 '10 at 4:31
55  
I get "fatal: Not a valid object name: 'origin/master'." –  joachim Mar 24 '11 at 22:07
42  
git push -u origin foo via –  Cotton Sep 21 '11 at 7:02

Another option is not to use the command line at all. Git GUI clients such as SourceTree take away much of the syntactical learning curve that causes questions such as this one to be amongst the most viewed on StackOverflow.

In SourceTree, simply right click on the local branch, select "Track remote branch >" and pick from the list.

share|improve this answer
    
Learning Git with an IDE = recipe for disaster –  Jubobs Mar 8 at 17:14
    
Personal preference I guess. In my opinion/experience, learning Git basics followed by using a combination of IDEs = recipe for success. –  Steve Chambers Mar 8 at 17:19

You might find the git_remote_branch tool useful. It offers simple commands for creating, publishing, deleting, tracking & renaming remote branches. One nice feature is that you can ask a grb command to explain what git commands it would execute.

grb explain create my_branch github
# git_remote_branch version 0.3.0

# List of operations to do to create a new remote branch and track it locally:
git push github master:refs/heads/my_branch
git fetch github
git branch --track my_branch github/my_branch
git checkout my_branch
share|improve this answer
1  
is grb a standard command? Available in which package? –  Jaseem Apr 20 '12 at 3:01
2  
grb is a ruby gem that can be accessed as explained on their github –  mcabrams Mar 18 '13 at 20:26
    
The OP is asking question about Git itself. So don't introduce a new tool would probably be better. –  zeekvfu May 18 '14 at 2:25

I believe that in as early as Git 1.5.x you could make a local branch $BRANCH track a remote branch origin/$BRANCH, like this.

Given that $BRANCH and origin/$BRANCH exist, and you've not currently checked out $BRANCH (switch away if you have), do:

git branch -f --track $BRANCH origin/$BRANCH

This recreates $BRANCH as a tracking branch. The -f forces the creation despite $BRANCH existing already. --track is optional if the usual defaults are in place (that is, the git-config parameter branch.autosetupmerge is true).

Note, if origin/$BRANCH doesn't exist yet, you can create it by pushing your local $BRANCH into the remote repository with:

git push origin $BRANCH

Followed by the previous command to promote the local branch into a tracking branch.

share|improve this answer
    
git push origin $BRANCH was what I was looking for. –  User Apr 2 '13 at 15:15
    
After trying all sorts of solutions, including setting up an upstream as described above nothing worked. All I wanted to do is pull 1 new commit into my local branch from a remote one and I didn't setup tracking initially. The command git branch -f --track $BRANCH origin/$BRANCH does the trick. –  DemitryT Feb 13 '14 at 15:08

Actually for the accepted answer to work:

git remote add upstream <remote-url>
git fetch upstream
git branch -f --track qa upstream/qa
# OR:
git branch --set-upstream qa upstream/qa
share|improve this answer
    
The local branch was already tracking a branch, so we can assume the remote repo was already added. –  Doppelganger May 18 '11 at 21:15
    
Dopplerganger: See joachim's comment to the accepted answer. Anyway assumptions readily differ - it what makes things so interesting ;) –  Hedgehog Jun 13 '11 at 23:20

In very short

git branch --set-upstream yourLocalBranchName origin/develop

This will make your yourLocalBranchName track the remote branch called develop.

share|improve this answer
    
@Quincy Check greg's answer - use git push -u origin branch (or --set-upstream-to) instead –  Tobias Kienzler May 28 '13 at 8:37
    
@MadNik, what is the difference between --set-upstream and --track? I don't quite understand why I should use one over the other. –  A-B-B Mar 31 '14 at 18:30

For 1.6.x, it can be done using the git_remote_branch tool:

grb track foo upstream

That will cause Git to make foo track upstream/foo.

share|improve this answer

You can do the following (assuming you are checked out on master and want to push to a remote branch master):

Set up the 'remote' if you don't have it already

# git remote add origin ssh://...

Now configure master to know to track:

# git config branch.master.remote origin
# git config branch.master.merge refs/heads/master

And push:

# git push origin master
share|improve this answer
    
it is really required the remote and the branch in the push? I mean, you only need it if your checked out branch is not the one you want to push, right ? –  Doppelganger Mar 2 '10 at 5:14
4  
Yes - but from memory you may need to be explicit for the first push. Can easily be tested of course... :) –  Paul Hedderly Jun 18 '10 at 20:57
    
+1 This is the answer for Windows users who are stuck with the msysgit "preview" that is pre 1.8. Thanks for that. –  John Oct 16 '12 at 12:33
2  
This is the only answer that worked for me. When I tried the accepted answer, to set the upstream remote for an existing branch, I got: error: the requested upstream branch 'upstream/master' does not exist. –  Steve K May 13 '14 at 23:02

I do this as a side-effect of pushing with the -u option as in

$ git push -u origin branch-name

The equivalent long option is --set-upstream.

The git-branch command also understands --set-upstream, but its use can be confusing. Version 1.8.0 modifies the interface.

git branch --set-upstream is deprecated and may be removed in a relatively distant future. git branch [-u|--set-upstream-to] has been introduced with a saner order of arguments.

It was tempting to say git branch --set-upstream origin/master, but that tells Git to arrange the local branch "origin/master" to integrate with the currently checked out branch, which is highly unlikely what the user meant. The option is deprecated; use the new --set-upstream-to (with a short-and-sweet -u) option instead.

Say you have a local foo branch and want it to treat the branch by the same name as its upstream. Make this happen with

$ git branch foo
$ git branch --set-upstream-to=origin/foo

or just

$ git branch --set-upstream-to=origin/foo foo
share|improve this answer

Make sure you run :

git config push.default tracking

to be able to push trouble free

share|improve this answer
    
This could be convenient. We could note, however, that according to git-config(1) manual page, tracking is deprecated synonym of upstream. –  FooF Aug 8 '12 at 2:39

Editing .git/config is probably the easiest and fastest way. That's what the Git commands for handling remote branches are doing, anyway.

If you don't want to muck with the file by hand (and it's not that hard to do), you can always use git config to do it...but again, that's just going to edit the .git/config file, anyway.

There are, of course, ways to automatically track a remote branch when using git checkout (by passing the --track flag, for example), but these commands work with new branches, not existing ones.

share|improve this answer

protected by BЈовић May 9 '12 at 13:33

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.