Skip to content
← Back to blog

10 September 2015

Nested Submodules in Git

I'm currently working on a project which has been split into a number of distinct parts, and as a result we have a number of separate Git repositories of code for the project. We decided to use submodules to manage…

I'm currently working on a project which has been split into a number of distinct parts, and as a result we have a number of separate Git repositories of code for the project. We decided to use submodules to manage these dependencies, and set it up so that the four repositories were nested, one inside the other, inside the other, inside the other.

When working with nested submodules, it's a little bit of a pain to get all of the submodules checked out and updated (assuming you want your projects to update as each dependant submodule changes). Lewis and I put our heads together and came up with this solution.

When you have cloned a project with submodules, first run this. It will initialise and subsequently clone each of the submodules:

git submodule update --recursive --merge --init

When you are ready to update the code to take into account changes to submodule code, first do a pull on your main repository:

git pull

Then tell Git to look recursively through each submodule, ensure we are tied to a branch and not running headless (which will prevent a pull), then pull the changes:

git submodule foreach --recursive 'git checkout master && git pull'

Now you can make use of nested submodules in your project and have changes down the line pulled all the way up to your main project.

Michael Peacock

Written by Michael Peacock

Consultant CTO & software engineer

Get in touch