In large enterprise projects, software applications rarely exist in isolation. You might build an e-commerce platform that shares a common payment library, custom UI components, or encryption module with five other internal applications. Git Submodules let you keep a Git repository as a subdirectory of another Git repository while preserving separate commit histories.
To include an external repository as a submodule inside your project:
# Add a submodule into the 'libs/payment-sdk' folder:
$ git submodule add https://github.com/company/payment-sdk.git libs/payment-sdk
When you run this command, Git creates two critical artifacts:
.gitmodules file: A plain text configuration mapping the submodule's local path to its remote repository URL:
[submodule "libs/payment-sdk"]
path = libs/payment-sdk
url = https://github.com/company/payment-sdk.git
If you run a standard git clone on a repository containing submodules, the submodule directories will be cloned as empty folders! To initialize and download their contents:
# Recommended: Clone and initialize all submodules in one command:
$ git clone --recurse-submodules https://github.com/company/main-app.git
# If you already cloned normally without the flag, run:
$ git submodule update --init --recursive
Because submodules are pinned to exact commit hashes, they do not update automatically when new commits are pushed upstream. To update them:
# Fetch and merge new commits for all submodules:
$ git submodule update --remote --merge
# Commit the updated commit pointer in the parent repository:
$ git commit -am "chore: update payment-sdk submodule to latest release"
While submodules store external links, Git Subtree merges the external project's files and commit history directly into your parent repository's tree. Subtrees avoid empty folder issues for colleagues, but require more complex merge commands.
Strengthen your version control mastery by studying these connected classes:
Understand how submodules record commit SHA-1 pointers inside parent trees.
Clone nested submodules recursively using git clone --recurse-submodules.
Evaluate submodules, subtrees, and monorepo architectures for enterprise codebases.
Add, update, initialize, and remove submodules in real projects.
Advance your software engineering skills with the next topic in our synchronized curriculum.
Continue to GitHub Tour & Ecosystem → Previous: ← PAT & Credentials | Full Syllabus