Git submodule: include a repo inside another
git submodule lets you include a Git repository inside another. Ideal for sharing code between projects or integrating external libraries.
What is git submodule?
git submodule lets you integrate a Git repository into another as a versioned dependency. The parent project points to a specific commit of the submodule.
It is like having a book within a book: the parent project contains a reference to the submodule, but each has its own history and its own commits.
Git submodule syntax
git submodule add url pathAdd a submodule
git submodule update --initInitialize and download
git submodule update --remoteUpdate to the latest commit
Git submodule in practice
Adding, cloning, and updating submodules.
Adding a submodule
Cloning and updating
Essential submodule commands
git submodule add urlAdd a new submodule
git submodule update --initInitialize after a clone
git submodule update --remoteUpdate to the latest commit
git submodule statusView submodule status
git submodule foreach cmdRun a command in each submodule
git submodule deinit pathDeinitialize a submodule
Common mistakes with git submodule
Empty folder after a clone
Submodules are not downloaded by default. Use git clone --recurse-submodules or git submodule update --init after the clone.
Forgetting to commit the update
After a git submodule update --remote, the new reference must be committed in the parent project. Otherwise, other developers will not see the update.
Difficulty removing a submodule
Removal requires several steps. Remember to deinit, rm, and clean up .gitmodules. It is one of the most confusing operations in Git.
Part of the Git Advanced guide
GitQuest is created by Anaïs (nouvelle fenêtre), web developer and head of education, specializing in tech training and digital accessibility.
Questions about git submodule
Master complex projects with GitQuest
Learn to manage submodules and multi-repo projects.
Start the investigations