How to git fetch everything

git fetch is a common command used to fetch and update the local repository with commits and branches from one or more remote repositories.

  • The simplest version of the command is:
$ git fetch

This fetches only from the origin remote. Also, note that it does not fetch for any of the submodules inside the current repository.

  • To fetch from a particular remote:
$ git fetch some_remote

This can be useful when there are many remotes and you want to fetch from just one to save time.

  • To fetch from all the remotes you have associated with the current repository:
$ git fetch --all

Note again, that this does nothing for the submodules.

  • To fetch from the origin remote of the main repository and the origin remote of all submodule repositories:
$ git fetch --recurse-submodules
  • What if we want to fetch from all remotes for the main repository and also from all remotes for all the submodules? We might think that this does that:
$ git fetch --all --recurse-submodules

And here you land into a Git trap! Strangely, the above command only fetches from all remotes for the main repository. For the submodules, it only fetches from their origin remote!

So, what if I do want to fetch from all remotes for all submodules? That can be achieved by using the very useful submodule foreach which loops over all submodules (but not the main repository!) and executes the git command you specify. Knowing this, we can do this:

$ git submodule foreach --recursive git fetch --all

We are almost there! Can we create one mega command to fetch from all remotes for both the main repository and all the submodules? We can do it at the shell by combining two commands:

$ git fetch --all && git submodule foreach --recursive git fetch --all
  • If you deal with submodules all the time, you can turn the above command into a Git alias by adding this to your .gitconfig:
[alias]
fetch-all-recur = !git fetch --all && git submodule foreach --recursive git fetch --all

With this alias added, you can sit back and type:

$ git fetch-all-recur

Enjoy! 🙂

Tried with: Git 2.8.2 and Ubuntu 14.04

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s