git submodule complete guide

January 26, 2026

What is Git Submodule

A git submodule embeds an independent Git repository inside another. The parent repo only stores two things:

  1. .gitmodules file - records submodule repo URL and path
  2. 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 CaseSolution
Third-party dependency (public)Package manager (pip/npm)
Need locked version + independent historySubmodule
Want dependency as "normal" codegit 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.toml using version deps (lib>=1.2.0), use editable install only for local dev.

Command Cheatsheet

TaskCommand
Add submodulegit submodule add <url> <path>
Clone with submodulesgit clone --recurse-submodules <url>
Initialize after clonegit submodule update --init --recursive
Update all to latestgit submodule update --remote --merge --recursive
Sync after branch switchgit submodule update --recursive
Check statusgit submodule status
Run command in allgit submodule foreach 'git status'
Remove submoduledeinit โ†’ 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: recursive in 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.0 in 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

ยฉ 2026 borui. All rights reserved. This content may be freely reproduced, displayed, modified, or distributed with proper attribution to borui and a link to the article: borui (2026-01-26). git submodule complete guide. https://caiborui.com/blog/2026-01-26-en-git-submodule-guide

@misc{borui2026-3682ca2b,
  author = {borui},
  title = {{git submodule complete guide}},
  year = {2026},
  publisher = {borui's blog},
  url = {https://caiborui.com/blog/2026-01-26-en-git-submodule-guide}
}