Join the AI Workshop to learn more about AI and how it can be applied to web development. Next cohort February 1st, 2026
The AI-first Web Development BOOTCAMP cohort starts February 24th, 2026. 10 weeks of intensive training and hands-on projects.
Git submodules let you include one Git repository inside another. This is useful when you want to incorporate external code while keeping it in its own repository.
Common Use Case
You have a website repository, but you want a portion of it (like documentation or a handbook) to be publicly editable on GitHub, separate from the main private repository.
Adding a Submodule
First, if you have existing content in the folder where you want the submodule:
rm -rf content/handbook
git commit -m "Remove handbook folder for submodule"
Then add the submodule:
git submodule add https://github.com/username/handbook
The submodule is now tracked in your repository.
How Submodules Work
When you add a submodule:
- Git clones the submodule repository
- Creates a
.gitmodulesfile tracking the submodule URL - Tracks which commit of the submodule your project uses
Your main repository doesn’t contain the submodule’s files—just a reference to a specific commit.
Deployment
Many deployment platforms (like Netlify) automatically detect and clone submodules when building your site.
Working with Submodules Locally
Submodules aren’t automatically symlinked. For local development, you might want to create a symlink to your local copy of the submodule:
# From within the content folder
ln -s ../../../dev/handbook/
Tell Git to stop tracking changes to this symlinked folder:
git update-index --skip-worktree content/handbook
To restore tracking later:
git update-index --no-skip-worktree content/handbook
Cloning a Repository with Submodules
When cloning a repository that has submodules:
git clone --recurse-submodules https://github.com/username/repo
Or if you already cloned without submodules:
git submodule init
git submodule update
Updating Submodules
To update a submodule to its latest commit:
cd submodule-folder
git pull origin main
cd ..
git add submodule-folder
git commit -m "Update submodule to latest"
Or update all submodules at once:
git submodule update --remote
Submodule Commands Reference
| Command | Description |
|---|---|
git submodule add <url> | Add a new submodule |
git submodule init | Initialize submodules after cloning |
git submodule update | Fetch submodule contents |
git submodule update --remote | Update to latest remote commits |
git submodule status | Show submodule status |
When to Use Submodules
Good use cases:
- Sharing code between multiple projects
- Including third-party libraries you want to version control
- Making part of a private repo publicly editable
- Separating concerns between different teams
Alternatives:
- Package managers (npm, pip, etc.) for dependencies
- Monorepos if you want everything in one repository
- Git subtrees for simpler embedding (though with different tradeoffs)
Submodules add complexity, so use them when you genuinely need separate repositories that reference each other.