How to pull or fetch all branches from git?

Published on Author Code Father

I’ve written my answer some time ago and last downvote motivated me to update it with my later experience 🙂

Edit: previous version of this answer created branches with ‘origin’ prefix, all pointing to master branch, instead of actual branches, and having problems with variable expansions. This has been fixed as per comments.

You can fetch one branch from all remotes like this:

git fetch --all

Fetch updates local copies of remote branches so this is always safe for your local branches BUTthat means:

  1. This will not create local branches tracking remote branches, you have to do this manually
  2. If you want to update your local branches you still need to pull every branch.

So if you will probably want to run:

git pull --all

However, this can be still insufficient. It will work only for your local branches which track remote branches. To track all remote branches execute this oneliner BEFORE git pull --all:

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

TL;DR version

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

(it seems that pull fetches all branches from all remotes, but I always fetch first just to be sure)

Run the first command only if there are remote branches on the server that aren’t tracked by your local branches.

P.S. AFAIK git fetch --all and git remote update are equivalent.



Categories Git