Git – Track All Remote Branches as Local Branches

branchgit

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

Output:

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 read the manuals, but have come up bunk thus far.

Best Answer

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 /HEAD`; do git checkout --track $remote ; done

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

Related Question