gitversion-controlgit-branchremote-branch

Git pull all branches from a remote repository


How do I pull all of the remote branches to my own repository?

If I type:

git branch -a

I get a long list of branches, but if I type:

git branch

I see only two of them.

How do I pull all branches into my local list?

I know I can do:

git checkout --track origin/branch-name

but that pulls and checks out only one branch at a time. Is there a way to get it all done at once without that whole tedious work of running git checkout --track origin/branch-name over and over and over again?


PS: I tried the following commands, but none of them made remote branches appear in my git branch list:

git fetch --all
git remote update
git pull --all

Solution

  • The command I usually use to make all visible upstream branches, and tracking them is detailed in "Track all remote git branches as local branches":

    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
    

    Or:

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

    For more readability:

    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; 
      # or
      git branch --track $brname  $remote/$brname ; 
    done
    

    The second one is for creating new local branches tracking remote branches.