DEV Community

Blair Vanderlugt
Blair Vanderlugt

Posted on

Demystifying Git Submodules

If you are working with Git submodules for the first time they may catch you off guard with their behavior.

Where are the submodules files after cloning? Why do they show changes when I ask git for status? Let's dig into the docs and figure out how to get started working with Git Submodules.

First, what are these submodules and why would I use them?

It often happens that while working on one project, you need >to use another project from within it. Perhaps it’s a library >that a third party developed or that you’re developing >separately and using in multiple parent projects. A common >issue arises in these scenarios: you want to be able to treat >the two projects as separate yet still be able to use one >from within the other.

The two tricky things you can encounter when you are first working with submodules are: initializing the submodule and pulling upstream changes.

When you first pull down a git repository containing submodules you will not actually get the submodule files with the clone. Kinda weird right? To get the files you can run

git submodule update --init

The "--init" command will populate the submodules with their respective git commit's.

The next hang up you might run into is when you pull down a branch an upon running "git status" you see that the submodules are modified with the message "(new commits)". This means your local branch does not contain the changes from upstream. To pull these run:

git submodule update

That should be enough to get you started working with Git submodules. For more information visit the Git Man page

Top comments (1)

therealchiko profile image
Chiko Mukwenha

Is this all or it's part of a series?