tags:

views:

11302

answers:

8

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.

EDIT It looks like this can't currently be done in a convenient way with the current (1.6.1.x) version of Git.

UPDATE Git version >= 1.7.0 supports this. See the accepted answer

+2  A: 

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.

mipadi
+6  A: 

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
floehopper
A: 

This worked best for me when creating new branches

Read Here: http://www.zorched.net/2008/04/14/start-a-new-branch-on-your-remote-git-repository/

+25  A: 

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

set up the 'remote' if you dont 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
Paul Hedderly
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
Yes - but from memory you may need to be explicit for the first push. Can easily be tested of course... :)
Paul Hedderly
+11  A: 

@mipadi doesn't really answer the question; Paul's answer is the most useful.


git config branch.local_branch_name.remote your_remote
git config branch.local_branch_name.merge refs/heads/remote_branch_name

git push
  • your_remote is usually called origin, particularly for GitHub users.
  • local_branch_name refers to the local branch you're wanting to set up to track the remote branch.
  • remote_branch_name is the remote branch that the local branch will track.
Matt Todd
+81  A: 

As of Git 1.7.0:

git branch --set-upstream foo upstream/foo

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

Dan Moulding
Yay! Very glad this has been added to 1.7.0.
Matt Todd
+1 nice to have that feature
gpampara
At last... :) still it's only doing what I suggested earlier...
Paul Hedderly
Is "upstream" the name of the remote? i.e. what most would call "origin" by default?
Andrew Vit
@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
+2  A: 

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

grb track foo upstream

That will cause Git to make foo track upstream/foo

p.s. all thanks to floehopper, I just thought to show exact command to fit this question ;)

wik
+1  A: 

I believe that in as early as git 1.5.x you can 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 (i.e. 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.

wu-lee