Skip to content

Tracking Remote Git Branches

Remote then Local

It is easy to setup a local branch (we’ll call it zzz here) to track a pre-existing remote branch:

$ git --track branch zzz origin/zzz

Alternately, if you use the branch.autosetupmerge setting in your git config to enable automatic branch tracking [1], the command is even shorter:

$ git branch zzz origin/zzz

Local then Remote (reverse)

This much is easy. But what about the reverse? In other words, how do you start with a local branch, push to a remote, and have the local track the remote?

Take One

Let’s give it a try. A warning: it isn’t quite as easy as you might hope.

$ git branch zzz
$ git checkout zzz
# make some changes to files ...
$ git push origin zzz
$ git pull origin zzz

The explicit form of git pull above works. But the shorter form “git pull” is easier, but when we try it we get an error message (but at least it is helpful):

$ git pull
From git@my-server.com:my_project
 * [new branch]      zzz -> origin/zzz
You asked me to pull without telling me which branch you
want to merge with, and 'branch.zzz.merge' in
your configuration file does not tell me either.	Please
specify which branch you want to merge on the command line and
try again (e.g. 'git pull <repository> <refspec>').
See git-pull(1) for details.

If you often merge with the same branch, you may want to
configure the following variables in your configuration
file:
    branch.zzz.remote = <nickname>
    branch.zzz.merge = <remote-ref>
    remote.<nickname>.url = <url>
    remote.<nickname>.fetch = <refspec>

See git-config(1) for details.

Adding those lines to the git config file by hand is doable, but not very elegant. You might rather use the command line way:

git config branch.zzz.remote origin
git config branch.zzz.merge refs/heads/zzz
# Note: I'm assuming you already have "origin" setup
# as a remote, so I don't include those commands here.

The latter way lends itself to scripting better than the former. Still, I wasn’t really satisfied with either approach.

Take Two

So I was curious to see if there are other ways to start with a local branch then create a remote branch and have automatic tracking. I did some searching and read over the following blog posts:

There was considerable diversity in the solutions presented.

Take Three

In my opinion, Mark Eli Kalderon’s post explains the simplest and most elegant approach. Here is his approach, slightly adapted:

$ git checkout -b zzz
# Let the hacking commence...
$ git push origin zzz
$ git checkout master # see note 2
$ git branch -f zzz origin/zzz
$ git checkout zzz
# Let the hacking continue...

The key to this approach is using the “-f” flag with “git branch” to force the re-creation of the local branch. It is short and easy to remember. If you find something simpler, please share it. Thanks.

Notes

  1. To enable automatic branch tracking globally, use:
    $ git config --global branch.autosetupmerge true
    

    To do it for a specific repository, use:

    $ git config branch.autosetupmerge true
    
  2. I added the “git checkout master” because otherwise the “git branch -f …” line would fail with this message: “fatal: Cannot force update the current branch.”
  3. It probably doesn’t matter for the purposes of this discussion, but I’m using git 1.6.2.3 installed via MacPorts (“sudo port install git-core”).

6 Comments

  1. I just use the grb gem and let it do the hard work for me:

    http://github.com/webmat/git_remote_branch

    Sunday, April 19, 2009 at 4:58 pm | Permalink
  2. Thanks for that, David. It’s fascinating that the original post (http://www.zorched.net/2008/04/14/start-a-new-branch-on-your-remote-git-repository) is actually easier to read, at least for those with ADD (everyone, basically). When you make sense of the information, it loses the fun somehow.

    Tuesday, July 14, 2009 at 9:38 am | Permalink
  3. Kristian wrote:

    I believe the first example should be:
    $ git branch –track zzz origin/zzz

    Thanks for a great guide.

    Thursday, July 23, 2009 at 10:06 am | Permalink
  4. Scratch that last comment. Your have compiled the best answers from everywhere. This find

    git branch -f zzz origin/zzz

    is great.

    Saturday, July 25, 2009 at 6:58 am | Permalink
  5. felix wrote:

    all good, but my problem was still this:

    having pushed the branch to the remote, the other machines were still unable to see this new branch using git branch -r

    so for those of us for whom this was not obvious: git branch -r lists the remote branches that are currently known. git fetch will fetch the branch information from the remote

    git fetch

    From ssh://123.45.67.890/home/moi/gits/gitpo
    * [new branch] pullproxy -> origin/pullproxy

    moi:~/gitpo$ git branch -r
    origin/HEAD
    origin/master
    origin/pullproxy

    now I can:
    git branch -f pullproxy origin/pullproxy

    and check it out:
    git checkout pullproxy

    Sunday, September 13, 2009 at 12:04 pm | Permalink
  6. kellyp wrote:

    Thanks for the super awesome post! They should try and make it a little more difficult to create a remote branch.

    Thursday, December 10, 2009 at 2:25 pm | Permalink

One Trackback/Pingback

  1. Tracking Remote Git Branches -being practical... on Saturday, August 29, 2009 at 3:26 pm

    [...] http://djwonk.com/blog/2009/04/18/tracking-remote-git-branches/ [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*