When to Use Git Submodules in Large Repositories
Decide when Git submodules fit a large repository, when partial clone, sparse checkout or packages are better, and how to roll one out safely.
Introduce Git submodules in a large repository only when a component has its own release cycle, its own owners or its own access rules, and the parent project must pin an exact version of it. If the real problem is clone size, slow checkouts or shared code that changes together, partial clone, sparse checkout or a package manager is usually the better fix.
This post is a decision guide. It helps you decide whether submodules are the right tool before you add one. For the hands-on commands (adding, cloning, updating, CI setup and removal), read our companion guide on how Git submodules help manage multi-repo DevOps architectures. For general repository hygiene, see the Git repository management best practices pillar.
Key takeaways
- A submodule is a pointer to one exact commit in another repository. That pinning is its main benefit and its main cost.
- The Git manual names three reasons to split a project into submodules: repository size, transfer size and access control.
- Submodules suit code that changes slowly and is released separately. They suit code that changes together with the parent poorly.
- Before you split a large repository, try partial clone, sparse checkout and Git LFS. They fix size problems without changing your workflow.
- Every submodule adds a step for every developer and every pipeline. Count that cost before you commit to it.
What a submodule really is
A submodule is a Git repository nested inside another one, the superproject. The superproject does not store the submodule's files. It stores two things: an entry in the .gitmodules file with the path and URL, and a special tree entry (a "gitlink") that records one commit ID. When someone runs git submodule update, Git checks out exactly that commit in the nested directory.
Two consequences follow, and every decision in this guide comes back to them:
- The parent never moves on its own. If the library repository gets fifty new commits, the superproject still points at the old one until someone updates the pointer and commits. That is great for reproducible builds and annoying for code you want to stay current.
- Histories stay separate. A change that touches both the parent and the submodule needs two commits in two repositories, often two pull requests, pushed in the right order.
The official gitsubmodules documentation describes this design as using another project while keeping its history independent, with the superproject choosing when to adopt new versions.
The signals that say "use a submodule"
Look for these conditions in your large repository. If two or more apply to the same directory, that directory is a reasonable submodule candidate.
1. The component has a separate release cycle
A shared protocol definition, a vendored third-party library you patch, or a firmware SDK that a separate team tags every quarter. The parent wants version 3.2 until it is ready for 3.3. Pinning a commit or tag is exactly what you want here.
2. Different people own it
If another team reviews and merges changes to the component, keeping it in their repository with their branch protection and their CODEOWNERS file is cleaner than sharing one giant repository with complex path rules.
3. Access must differ
Some users may see the main application but not a licensed codec, a customer-specific configuration or a security module. Git has no per-directory read permissions inside one repository. The Git manual lists access control as a reason to split, because each submodule repository can have its own read and write policy.
4. The content is large and rarely needed
Large binary assets, test fixtures or documentation builds that most developers never touch can live in a submodule that people only initialise when they need it. The manual notes that such a submodule can also be cloned shallowly.
5. Several superprojects consume the same code
If three products embed the same internal library at different versions, each product's superproject can pin its own commit. Nobody is forced to upgrade at the same time.
The signals that say "do not use a submodule"
- The code changes together with the parent most weeks. If most features touch both, you will spend your time coordinating paired commits. Keep it in one repository.
- The only problem is size or clone speed. Git has features for that which do not split history (see the next section).
- A package manager already exists for the language. npm, Maven, pip, Go modules and NuGet handle versioned dependencies with better tooling for upgrades, ranges and security scanning.
- Your team is new to Git. Detached HEAD states, forgotten
--recurse-submodulesflags and unpushed submodule commits are the most common sources of confusion. - You need atomic changes across components. One commit cannot change both repositories. If a refactor must land everywhere at once, a single repository is simpler.
Try these first: fixing size without splitting
Many teams reach for submodules because a repository has become slow. Before you split it, check whether one of these built-in features solves the actual pain.
| Problem | Feature to try | What it does |
|---|---|---|
| Clone downloads years of large file versions | Partial clone (git clone --filter=blob:none) | Fetches commits and trees, and downloads file contents only when they are needed |
| Developers only work in a few directories | git sparse-checkout | Checks out only the directories you list, so the working tree stays small |
| CI only needs the latest code | Shallow clone (--depth 1) | Skips history entirely for build jobs |
| Large binaries bloat history | Git LFS | Stores large files outside Git history and keeps small pointer files in the repository |
| Very large repository on many machines | scalar | Part of the Git suite; turns on settings recommended for large repositories, such as background maintenance |
A quick example that combines two of them for a developer who only works on one service:
git clone --filter=blob:none --sparse https://example.com/org/platform.git
cd platform
git sparse-checkout set services/payments shared/proto
See the official git sparse-checkout and partial clone documentation for the details. If these fix the problem, you keep one history, one pull request per change and no extra workflow steps.
Submodules versus the alternatives
When a split really is needed, compare the options side by side.
| Approach | Version pinning | Extra steps for developers | Best fit |
|---|---|---|---|
| Single repository (monorepo) | Not needed, everything is at one commit | None | Code that changes together, atomic refactors |
| Git submodule | Exact commit, updated on purpose | Clone and update with submodule flags | Separately owned or access-restricted code |
| Git subtree | Copied into parent history | Few for readers, more for maintainers who sync upstream | Vendored code that is rarely pushed back |
| Package manager | Version ranges or lock files | Normal install step | Libraries with a published release process |
For a wider look at repository structure, read the trade-offs between monorepo and polyrepo structures. Note that git subtree lives in Git's contrib area, so check that it is installed on your machines before you standardise on it.
A decision checklist
Answer these questions for the directory you want to move out. Count the "yes" answers.
- Does a different team own and review this code?
- Does it ship on its own schedule, with its own tags?
- Do some people need access to the parent but not to this code, or the reverse?
- Do two or more projects consume it?
- Do fewer than one in five changes touch both this code and the parent?
- Have you already tried partial clone, sparse checkout or LFS and still have a problem?
- Can you update your CI pipelines and onboarding docs to handle submodules?
Five or more "yes" answers: a submodule is a sound choice. Three or four: consider a package or a subtree first. Two or fewer: keep the code where it is and fix the specific pain point instead.
Costs to plan for before you introduce one
Developer workflow
A plain git clone leaves submodule directories empty. People need git clone --recurse-submodules, or git submodule update --init --recursive after the fact. Setting submodule.recurse to true makes commands such as checkout and pull recurse by default. The Git manual points out that git clone still needs the explicit flag even with that setting.
git config --global submodule.recurse true
git clone --recurse-submodules https://example.com/org/app.git
CI/CD pipelines
Every pipeline that builds the superproject must fetch submodules, and it needs credentials for each submodule repository. Private submodules on a different host or in a different organisation are a common cause of failed builds. Decide up front how your runners will authenticate. Our guide on tools for faster CI/CD pipelines covers caching that helps keep repeated submodule fetches cheap.
Change coordination
A fix inside a submodule follows this order: commit and push in the submodule, then commit the new pointer in the superproject and push that. If someone pushes the parent first, everyone else gets a pointer to a commit that does not exist on the server. Running git push --recurse-submodules=check makes Git refuse that push, and on-demand pushes the submodule first.
Review visibility
In a pull request, a submodule bump often shows as a single line that changes one commit ID to another. Reviewers cannot see what changed inside. Ask authors to link the submodule's changelog or compare view in the pull request description.
How to introduce a submodule safely
If the checklist says yes, roll it out in stages rather than in one big switch.
- Extract with history. Create the new repository from the existing directory so blame and log still work. Tools such as
git filter-repo(a separate project recommended in the git filter-branch manual) can keep only that path's history. - Tag a first release in the new repository so the parent pins something meaningful.
- Replace the directory in the parent with
git submodule addpointing at that tag, in one reviewed pull request. - Update CI in the same pull request so the main branch never breaks.
- Update onboarding docs with the clone command and the
submodule.recursesetting. - Agree an upgrade rhythm, for example a weekly or per-release bump, owned by a named person or automated with a dependency update bot.
Governance rules that keep submodules manageable
- Pin to tags or reviewed commits on the submodule's main branch, never to a feature branch commit.
- Use HTTPS URLs or relative URLs in
.gitmodulesso both people and CI can resolve them. - Keep nesting shallow. Submodules inside submodules multiply every problem above.
- Add a CI check that fails if a submodule pointer references a commit that is not reachable on the remote.
- Review the list of submodules once a quarter. If one now changes in step with the parent every week, consider moving it back.
Large teams that use these rules often combine them with the branching and review habits described in Git strategies used by large-scale teams. For the everyday commands you will run around submodules, keep the top Git commands reference handy.
Worked example: splitting a product repository
Consider a hypothetical repository that holds a web application, a mobile app, a shared API schema and 4 GB of design assets.
- Design assets: large, rarely needed by engineers, owned by the design team. Two signals for a split, but Git LFS plus sparse checkout fixes the size issue with less overhead. Start there.
- Shared API schema: consumed by the web app, the mobile app and a partner SDK in another repository, versioned with tags, reviewed by the platform team. This one is a strong submodule candidate, or a published package if your languages support it.
- Web and mobile apps: different teams, but features often ship in both on the same day. Keep them in one repository, or split them as separate repositories that each consume the schema. Do not nest one inside the other.
The point of the example: in one large repository the right answer can differ per directory. Decide component by component.
Learn this hands-on
Repository design, CI pipelines and Git workflows are part of our DevOps training, run as classroom sessions in Kharadi, Pune and live online across India. If your pipelines run on GitLab, the GitLab training covers submodule handling in GitLab CI/CD.
Frequently asked questions
When should I introduce Git submodules in a large repository?
When a component has its own owners, its own release cycle or its own access rules, and the parent must pin an exact version. If you only want a smaller or faster repository, try partial clone, sparse checkout or Git LFS first.
Do submodules make a large repository faster to clone?
They can, because developers skip submodules they do not initialise. Partial clone and sparse checkout often give the same gain without splitting history or adding steps.
What is the biggest downside of Git submodules?
Coordination. Changes across the parent and a submodule need separate commits pushed in order, and every developer and pipeline must fetch submodules explicitly.
Are submodules better than a monorepo?
Neither is better in general. A monorepo suits code that changes together and needs atomic refactors. Submodules suit separately owned, separately versioned code.
Should I use a submodule or a package manager?
If the code is a library in a language with a mature package manager, publish it as a package. Use a submodule when there is no good package format or you need the source tree itself.
How is git subtree different from a submodule?
A subtree copies the other project's files into the parent's history, so readers need no extra commands. A submodule stores only a pointer to a commit in a separate repository.
Can submodules restrict who sees certain code?
Yes. Each submodule is its own repository with its own permissions, so users without access to it can still work on the parent. Git cannot restrict read access per directory inside one repository.
How do I stop people pushing a parent commit that points to an unpushed submodule commit?
Use git push --recurse-submodules=check, or set push.recurseSubmodules to check, and add a CI check that the pinned commit exists on the remote.
References
What's Your Reaction?
Like
0
Dislike
0
Love
0
Funny
0
Angry
0
Sad
0
Wow
0