Track all remote git branches as local branches

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.


Using bash:

after git 1.9.1
for i in `git branch -a | grep remote | grep -v HEAD | grep -v master`; do git branch --track ${i#remotes/origin/} $i; done

credits: Val Blant, elias, and Hugo

before git 1.9.1

Note: the following code if used in later versions of git (>v1.9.1) causes

  • (bug) All created branches to track master
  • (annoyance) All created local branch names to be prefixed with origin/
  • 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.


    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.


    Here is my one-liner I use (in a bash shell, tested with msysgit1.7.4):

    For copy-paste:

    remote=origin ; for brname in `git branch -r | grep $remote | grep -v master | grep -v HEAD | awk '{gsub(/^[^/]+//,"",$1); print $1}'`; do git branch --set-upstream-to $remote/$brname $brname; done
    

    For more readibility:

    remote=origin ; // put here the name of the remote you want
    for brname in `
        git branch -r | grep $remote | grep -v master | grep -v HEAD 
        | awk '{gsub(/^[^/]+//,"",$1); print $1}'
    `; do 
        git branch --set-upstream-to $remote/$brname $brname; 
    done
    
  • it will only select upstream branches from the remote you specify in the remote variable (it can be ' origin ' or whatever name you have set for one of the remotes of your current Git repo).
  • it will extract the name of the branch: origin/a/Branch/Name => a/Branch/Name through the awk expression.
  • it will set the upstream branch through --set-upstream-to (or -u ), not --track :
    The advantage is that, if the branch already exists, it won't fail and it won't change that branch origin, it will only configure the branch.xxx.(remote|merge) setting.

    branch.aBranchName.remote=origin
    branch.aBranchName.merge=refs/heads/a/Branch/Name
    
  • That command will create local branches for all remote upstream branches, and set their remote and merge setting to that remote branch.

    链接地址: http://www.djcxy.com/p/26146.html

    上一篇: 如何看待使用git的另一个开发人员的分支?

    下一篇: 跟踪所有远程git分支作为本地分支