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:
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

Slow speed on git clone

Sometimes when I have to clone a huge repository from Github, I find that the fetch speed can be extremely slow (KB/s).

There are two tricks I try for a quick cloning:

  • Kill the command and try again. Do this a few times, see if you can get a faster connection. This works most of the times.

  • Fetch only the latest revision first and then fetch the rest:

$ git clone --depth=1
$ cd hello-world
$ git fetch --unshallow

Tried with: Git 1.9.1 and Ubuntu 14.04