How to git pull all branches


TL;DR: To sync all local git branches with remote origin:

$ git branch --format='%(refname:short)' | xargs -I {} sh -c 'git checkout {}; git pull'

Problem

Let’s say you have the following branches for your git repository:

$ git branch
  foo
  bar
  baz
* master

If you wanted to sync all your local branches with remote origin, you might do something like this:

$ git checkout foo
$ git pull
$ git checkout bar
$ git pull
$ git checkout baz
$ git pull

Wouldn’t it be nice to automate this with a one-line script?

Solution

First, you want to list all your git branches:

$ git branch
  foo
  bar
  baz
* master

An asterisk is prepended on the current checked out branch.

To remove it, you can pass the option --format='%(refname:short)':

$ git branch --format='%(refname:short)'
foo
bar
baz
master

Then pipe the branch names to xargs:

xargs -I {} sh -c # 'echo {}'

And checkout and pull:

git checkout {}; git pull

This leaves us with the final command:

$ git branch --format='%(refname:short)' | xargs -I {} sh -c 'git checkout {}; git pull'


If you enjoyed this post, please consider supporting this site!