What is Git Submodule
A git submodule embeds an independent Git repository inside another. The parent repo only stores two things:
.gitmodulesfile - records submodule repo URL and path- A commit pointer - locks submodule to a specific commit
The parent does NOT store submodule's full history. This is why submodules can seem "empty" if not properly initialized.
Basic Operations
Adding a submodule
git submodule add https://github.com/user/lib-project.git libs/lib-project
git commit -m "Add lib-project as submodule"
Cloning a repo with submodules
# Best: clone and init submodules in one command
git clone --recurse-submodules <repo_url>
# If already cloned without submodules
git submodule update --init --recursive
Updating Submodules
Manual update (update submodule to its latest)
cd libs/lib-project
git checkout main
git pull
# Return to parent and commit the pointer change
cd ../..
git add libs/lib-project
git commit -m "Bump lib-project submodule"
One-liner update all submodules
git submodule update --remote --merge --recursive
Common Pitfalls
Pitfall 1: Empty submodule directory after clone
Cause: Submodules not initialized.
Fix:
git submodule update --init --recursive
Pitfall 2: Wrong submodule version after branch switch
Cause: Different branches record different submodule commits.
Fix:
git submodule update --recursive
Pitfall 3: Detached HEAD in submodule
Cause: Parent locks to a commit, not a branch. This is expected behavior.
Fix (if you need to develop in submodule):
cd libs/lib-project
git checkout main
GitHub Integration
HTTPS vs SSH
Use SSH for team projects:
git submodule add git@github.com:user/lib-project.git libs/lib-project
GitHub Actions CI
Add to .github/workflows/xxx.yml:
- uses: actions/checkout@v4
with:
submodules: recursive
Private repo submodules
For private submodules, add token:
- uses: actions/checkout@v4
with:
submodules: recursive
token: ${{ secrets.PAT_TOKEN }}
Configure PAT_TOKEN in repo Settings โ Secrets and variables โ Actions.
Deleting a Submodule
git submodule deinit -f libs/lib-project
git rm -f libs/lib-project
rm -rf .git/modules/libs/lib-project
git commit -m "Remove submodule lib-project"
Submodule vs Package Manager
| Use Case | Solution |
|---|---|
| Third-party dependency (public) | Package manager (pip/npm) |
| Need locked version + independent history | Submodule |
| Want dependency as "normal" code | git subtree |
When both need publishing (pip/npm)
If both parent and submodule are packages you publish:
- Production: Parent depends on published version (
lib>=1.2.0) - Development: Use editable install for instant changes
pip + venv editable workflow
# Create and activate venv
python -m venv venv
source venv/bin/activate
# Install submodule as editable (changes take effect immediately)
pip install -e ./libs/lib-project
# Install main project as editable
pip install -e ./main-project
Now editing libs/lib-project code reflects immediately in main project - no reinstall needed.
โ ๏ธ Warning: Never put local paths in published dependencies. Keep
pyproject.tomlusing version deps (lib>=1.2.0), use editable install only for local dev.
Command Cheatsheet
| Task | Command |
|---|---|
| Add submodule | git submodule add <url> <path> |
| Clone with submodules | git clone --recurse-submodules <url> |
| Initialize after clone | git submodule update --init --recursive |
| Update all to latest | git submodule update --remote --merge --recursive |
| Sync after branch switch | git submodule update --recursive |
| Check status | git submodule status |
| Run command in all | git submodule foreach 'git status' |
| Remove submodule | deinit โ git rm โ rm .git/modules/... โ commit |
Quick Q&A
Q: What is git submodule?
A: A way to embed an independent Git repo inside another. Parent repo only stores a .gitmodules file and a commit pointer - not the full submodule history. Useful for shared libraries you want to lock at specific versions.
Q: How to use submodule with GitHub?
A:
- Use SSH URLs for team projects:
git@github.com:user/repo.git - Add
submodules: recursivein GitHub Actions checkout - For private submodules, configure PAT token in repo secrets
Q: Both parent and submodule need to publish to pip/npm?
A: Don't use submodule as the delivery mechanism. Use it only for development:
- Production: Parent depends on published version (
lib>=1.2.0in pyproject.toml) - Development: Use editable install (
pip install -e) or npm workspaces/link
This way you get: instant local changes + reproducible published packages.
Q: I want to fix bugs in submodule without going through publish cycle?
A: Use editable install (Python) or workspace/link (npm):
Python:
pip install -e ./libs/lib-project # Changes reflect immediately
pip install -e ./main-project
npm (workspaces):
{ "dependencies": { "@you/lib": "workspace:*" } }
npm (separate repos):
cd lib-project && npm link
cd main-project && npm link @you/lib
Key: Keep published dependencies using version numbers, use local linking only for development.
Q: When should I NOT use submodule?
A:
- For third-party libs with good package manager support โ use pip/npm
- When you want simpler workflow and don't need independent history โ use git subtree
- When packages are tightly coupled and always release together โ use monorepo