Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

How to git fetch everything

📅 2016-May-17 ⬩ ✍️ Ashwin Nanjappa ⬩ 📚 Archive

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

$ 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.

$ git fetch some_remote

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

$ git fetch --all

Note again, that this does nothing for the submodules.

$ git fetch --recurse-submodules
$ 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
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