tags:

views:

3762

answers:

3

Tracking a single remote branch as a local branch is straightforward enough.

$ git checkout --track -b ${branch_name} origin/${branch_name}

Pushing all local branches up to the remote, creating new remote branches as needed is also easy.

$ git push --all origin

I want to do the reverse. If I have X number of remote branches at a single source:

$ git branch -r 
branch1
branch2
branch3
.
.
.

Can I create local tracking branches for all those remote branches without needed to manually create each one? Say something like:

$ git checkout --track -b --all origin

I've googled and RTMs, but have come up bunk thus far.

+9  A: 

You could script that easily enough, but I don't know when it'd be valuable. Those branches would pretty quickly fall behind, and you'd have to update them all the time.

The remote branches are automatically going to be kept up to date, so it's easiest just to create the local branch at the point where you actually want to work on it.

Dustin
That's a good point. The main use case I'm thinking of is setting up a local dev environment based on a remote git repository. So when I do my initial clone, I also want to track all the remote branches as they are at that time.
Janson
+14  A: 

Using bash:

for remote in `git branch -r `; do git branch --track $remote; done

Update the branches, assuming there are no changes on your local tracking branches:

for remote in `git branch -r `; do git checkout $remote ; git pull; done

Ignore the ambiguous refname warnings, git seems to prefer the local branch as it should.

Otto
Thanks for the heads-up about the refname warnings. That was helpful.
Paul
Thanks Otto, I suspected that scripting would be the only solution. You've provided a pretty simple one.
Janson
A: 

The answer given by Otto is good, but all the created branches will have "origin/" as the start of the name. If you just want the last part (after the last /) to be your resulting branch names, use this:

for remote in `git branch -r | grep -v master `; do git checkout --track $remote ; done

It also has the benefit of not giving you any warnings about ambiguous refs.

Tim
Git will not add "origin" to the local tracking branch name.
adymitruk
@adymitruk: Actually it behaves exactly as I said for me on OSX 10.6.4, using git 1.7.2.2 (the latest stable as of this comment). Otto even mentions ambiguous refname warnings - the warnings wouldn't need to exist if "origin/" wasn't part of each local branch name. Here's the 'git branch' output after running Otto's command: [master, origin/HEAD, origin/charts, origin/master, origin/production, origin/staging]. And my command: [charts, master, production, staging].
Tim