git pull all branches from remote repository

Read e.g. this explanation http://git-scm.com/book/en/Git-Branching-Remote-Branches

First let's clarify some git terminology:

  • fetch: getting contents (or updates) from a remote repo
  • pull: fetch (as above) and merge in one step

The original poster did not mention merging, so I might guess in proper git terminology he might even have wanted to ask "git fetch all branches from remote repository"

If you see the branches in git branch -a then you have already fetched them. You can verify this by giving the command git show remotes/origin/some-branch:some-file

Or can do e.g. git diff remotes/origin/some-branch master

You can even check them out git checkout remotes/origin/some-branch

(To be sure you can remove the network cable and you will see that the commands work without contacting the remote repo.)

The branches named remotes/... are called remote branches, but they are already fetched to your repo. They are read-only, you cannot modify them (that's why a message appears when checking out). Although they reflect the state of a remote repo at the time of the last fetch or pull operation they are in fact stored locally.

If you do git checkout some-branch and some-branch does not yet exist but remotes/origin/some-branch exists, git will create a tracking branch called some-branch for you (1). Again this is a local operation, all data has been fetched before (or if you have not recently fetched, you will start working on an obsolete version). Originally the contents of the tracking branch is identical to its remote branch. However, the tracking branch can be modified by you locally.

The git working area contains the state of one branch at time. So your question about checking out all remote branches at once does not really make sense in the context of git. You can check out them one after each other. But each time you check out the next one, the previous one will disappear from the working area. Of course this operation can be scripted as shown in Track all remote git branches as local branches But what is the point of scripting a mass operation if only its last step is what remains?

So could the question be caused by a misunderstanding, assuming that remote branches would be only stored remotely, but not locally and you just wanted to make sure that you have everything local? If you really want to have more than one branch checked out at a time you can clone your repo locally and checkout different branches into different work areas. (2)

Shortly: If you want to be sure that you have all data available locally that is in the remote repo just use git fetch [repo]. Unless you have tweaked with your configuration this will fetch all branches, i.e. updating existing remote branches and also creating new remote ones if applicable.

(1) This is true in simple standard cases. In more complicated cases with more than 1 remote or manually configured remotes you might need the --track option to specify exactly what you want.

(2) There is a new feature git worktree for this use case. However as of early 2018 it is still marked experimental


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.


I've always used this and it works perfectly

git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
git fetch --all
git pull --all