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
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:
- Start a New Branch on your Remote Git Repository by Geoff Lane
- Pushing and Tracking Remote Branches with Git by Mark Eli Kalderon
- Git Push – Just the Tip by Rein Henrichs
- Improving my Git Workflow by Trevor Squires
- Git Branch Auto Tracking by Mike Champion
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
- 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
- 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.”
- 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
I just use the grb gem and let it do the hard work for me:
http://github.com/webmat/git_remote_branch
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.
I believe the first example should be:
$ git branch –track zzz origin/zzz
Thanks for a great guide.
Scratch that last comment. Your have compiled the best answers from everywhere. This find
git branch -f zzz origin/zzz
is great.
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
Thanks for the super awesome post! They should try and make it a little more difficult to create a remote branch.
2 Trackbacks/Pingbacks
[...] http://djwonk.com/blog/2009/04/18/tracking-remote-git-branches/ [...]
[...] Alternative methods listed here. [...]
Post a Comment