Git Submodules
Git Submodules: Embedding One Git Repo Inside Another
A Git submodule lets you embed an external Git repository inside a subdirectory of your main repository while keeping each project's commit history completely separate. Instead of copying third-party code by hand, your parent repository records a pinned commit hash pointing to the exact version of the nested repo your app expects.
Adding a Submodule to Your Repository
Suppose you want to include a shared UI design tokens repository inside vendor/design-tokens:
git submodule add git@github.com:org-name/design-tokens.git vendor/design-tokens
git status
git commit -m "Add design-tokens submodule"
Git creates two things: a .gitmodules config file mapping the folder path to the remote URL, and a special gitlink entry storing the exact commit SHA currently checked out in vendor/design-tokens.
Cloning a Project That Uses Submodules
Running a plain git clone creates empty submodule directories by default. Always pass --recurse-submodules when cloning:
git clone --recurse-submodules git@github.com:your-username/starter-site.git
If you already cloned without that flag, populate and check out all submodules afterward with:
git submodule update --init --recursive
Pulling New Commits into a Submodule
Submodules stay pinned in detached HEAD at their recorded commit until you explicitly update them and commit the new pointer in the parent repository:
git submodule update --remote --merge
git add vendor/design-tokens
git commit -m "Bump design-tokens submodule to latest main"
Common Submodule Mistakes
Editing files inside a submodule directory while in detached HEAD and pushing only the parent repository leaves teammates unable to fetch your submodule commit. Always commit and push inside the submodule directory first, then commit the updated pointer in the parent repo.
Frequently Asked Questions
When should I use a package manager (npm/Composer) instead of a Git submodule?
Prefer npm, Composer, pip, or Cargo for versioned libraries whenever possible. Use submodules mainly for shared internal assets, themes, or C/C++ source dependencies that lack a package registry.
Why is my submodule folder empty after cloning?
You ran git clone without --recurse-submodules. Run git submodule update --init --recursive inside the cloned directory to populate it.
How do I remove a submodule cleanly?
Run git submodule deinit -f vendor/design-tokens, remove its .git/modules/vendor/design-tokens cache, and run git rm -f vendor/design-tokens, then commit the change.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With