If you’ve ever tried to add a Git repository within another Git repository, you may have gotten a confusing message like this:
It mentions submodules, but what are they?
Git submodules are a pretty simple concept. They’re a way to reference a Git repo within another repo.
They work similarly to a link in a website. It would be tedious and difficult to copy a whole webpage and include it in yours, so instead you’d create a link to the other page.
Creating a submodule
Let’s assume you’re inside the parent repository, and you want to add a submodule. Since this project requires another library, you want to include it in yours. To do this, use the following command:
To include the Linux kernel as a submodule in the directory
kernel, you could use this command:
Adding a submodule will create the file
.gitmodules in the root directory of your repository.
If you clone a repository that uses submodules, they won’t be cloned unless you use the
If you’re already working in the repository, there are several options to the
git submodule command.
This will clone all submodules into their respective locations.
Updates all submodules.
--remote option, it will update all submodules to the latest local copy of the remote repo.
--remote, it will first fetch the remote repo.
This is equivalent to running
git pull in each submodule.
This can be combined with the
This will update all existing submodules, as well as cloning new ones.
This is just a quick guide to help you get started with submodules. See the gitsubmodules(7) man page for a complete explanation.