Git Submodules

15/07/2019

Creating a submodule

Creating a git submodule is pretty easy:

$ git submodule add [path_to_repo] [Name_of_Submodule]

In the below picture, you can see that I created a submodule from a remote repo and put it in the Submodules folder.

What I did was, I created a private repo called TestSubmodules on GitHub and then created another private repo Repo2. I added Repo2 as a submodule to TestSubmodules and as you can see, it moved Repo2 into the SubmoduleRepo folder.

I, however, could not add a submodule to a local repository that was on my computer without a remote. (It was worth the test, for science!)

If you have a older version of Git, you may have to run the below command to get it to download the contents of the submodule:

git submodule update --init --recursive

After commiting and pushing up to GitHub, you will see this:

Cloning a repo with a submodule

When cloning a git repo with a submodule, simply cloning with git clone [path_to_git_repo] will not pull any of the submodules.

You will see a folder for your submodule but it will be empty!

Instead, use:

git clone --recursive [path_to_git_repo]
Cloning a repository with submodules

This will pull your submodules as well.

Last updated

Was this helpful?