Sub topic 12
Choosing between submodules and subtrees
This chapter assumes you are comfortable with everyday Git (commit, branch, merge, push, pull), know that a commit hash identifies one exact snapshot, and can use a terminal. It also assumes you have seen the basic commands for both tools: git submodule add, git submodule update --init --recursive, git subtree add and git subtree pull. Here the goal is to decide which tool fits a given project, and to avoid the mistakes that make people regret the choice.
What each tool actually stores
The two tools differ in what ends up in your repository's object database.
A submodule stores a pointer. The parent repository's tree contains a special entry, called a gitlink, with mode 160000. The entry holds the path (vendor/libfoo) and one commit hash from the other repository. The file .gitmodules adds the URL to clone from. None of libfoo's files or history are in the parent's objects. They live in a separate repository, which Git keeps under .git/modules/vendor/libfoo and checks out into the working tree.
A subtree stores content. After git subtree add, libfoo's files are ordinary blobs and trees inside your repository. With --squash, the upstream history is collapsed into one commit. Without it, the upstream commits are merged into your history. In both cases, a clone of your repository contains everything. There is no .gitmodules file, no gitlink, and no second repository to initialize.
Most of the practical differences below follow from this one fact: a submodule is a reference, and a subtree is a copy.
Comparing day-to-day behavior
| Concern | Submodule | Subtree |
|---|---|---|
| Clone | Needs `--recurse-submodules` or a later `git submodule update --init --recursive` | Plain `git clone` is enough |
| Repository size | Parent stays small; each submodule is a separate clone | Parent grows by the size of the embedded code (and history, unless squashed) |
| Extra files | `.gitmodules` and `.git/modules` | None. It looks like normal directories |
| Version pinning | Exact commit recorded in the parent | Exact content recorded in the parent's commits |
| Updating | `git submodule update --remote`, then commit the new pointer | `git subtree pull`, which creates a merge commit |
| Editing the dependency | Commit and push inside the submodule first, then record the pointer | Edit like any other file; push back later with `git subtree push` |
| Offline or upstream deleted | Fails if the pinned commit is unreachable | Works, because the content is already in your repository |
| Tooling support | Needs CI and IDE configuration to fetch submodules | Works with any tool that understands a plain repository |
| Required knowledge for contributors | Everyone must learn submodule commands | Only the maintainer who syncs upstream must learn subtree commands |
Where submodules fit well
Choose submodules when the other repository has its own life and you want a clear boundary.
- Separate ownership. Another team publishes the library and you consume specific releases. The pointer shows exactly which commit you use, and the history of the library stays in the library's repository.
- Large dependencies. The embedded project is big, or has a long history, and you do not want that weight in every clone of the parent. Submodules also support shallow fetches (
git submodule update --depth 1) and let people skip submodules they do not need. - Frequent work on both sides. You often change the dependency and the parent together. Because the submodule is a real repository, you commit, branch and push in it with ordinary commands.
- Strict traceability. The parent's history records an exact upstream hash and nothing else. Review is simple: a diff of the parent shows a one-line change from one hash to another.
Where subtrees fit well
Choose subtrees when you want the dependency to behave like part of your project.
- Simple onboarding. Contributors clone, build and push without learning anything extra. This matters for open source projects with occasional contributors, and for teams that mix experience levels.
- Vendored code. You want a self-contained repository that builds with no network access beyond the clone, perhaps for audits, air-gapped builds, or protection against an upstream repository disappearing.
- Local patches. You modify the dependency and rarely send changes back. Local edits are normal commits, and later
git subtree pullmerges upstream changes with them. - CI without special steps. Many hosting services and build tools do not fetch submodules by default. A subtree needs no configuration at all.
Failure modes to expect
Each approach has predictable problems. Knowing them helps you decide whether you can live with them.
Submodule problems
- Empty directory after a plain clone. The path exists but has no files. Run
git submodule update --init --recursive. - Detached HEAD inside the submodule.
git submodule updatechecks out the recorded commit directly, not a branch. Commits made there are easy to lose unless you first rungit switchto a branch. - Unpushed submodule commit. You commit in the submodule, record the new pointer in the parent and push the parent. Colleagues then fail to fetch a commit that exists only on your machine. Use
git push --recurse-submodules=check(oron-demand) to prevent this. - Forgotten pointer. You change the submodule's checkout but never commit the new pointer in the parent.
git statusshowsmodified: vendor/libfoo (new commits), and a teammate silently builds a different version. - Branch switching. Switching branches in the parent does not move submodule checkouts unless you run
git submodule updateor setsubmodule.recurseto true. - Moved URL. If the upstream URL changes, you need to edit
.gitmodulesand rungit submodule sync.
Subtree problems
- History noise. Without
--squash, every upstream commit enters your history. With--squash, you keep your log tidy but lose per-commit blame for the vendored code. - Inconsistent squash use. Mixing squashed and unsquashed operations on the same prefix often produces confusing merges or conflicts. Pick one and use it every time.
- Easy to forget the origin. Nothing in the repository says where the code came from or which upstream version it matches, except the commit messages that
git subtreewrites. Document the URL and branch, for example in a README next to the directory. - Mixed commits. A commit that changes both your code and
vendor/libfoocannot be pushed upstream cleanly.git subtree pushhas to rewrite history to extract the directory's commits, and mixed commits get split or skipped. Keep vendored-code edits in separate commits. - Slow push and split. On large histories,
git subtree splitwalks the whole history and can take minutes. - Silent divergence. Because local edits look like normal changes, the copy can drift from upstream without anyone noticing, which makes later merges harder.
A decision procedure
Work through these questions in order. Stop at the first one that gives a firm answer.
- Do contributors need to avoid learning extra Git commands? If yes, use a subtree.
- Must the repository build with a single plain clone, offline, or in a CI system that cannot fetch other repositories? If yes, use a subtree.
- Is the dependency large, with a long history, and not needed by everyone? If yes, use a submodule.
- Do you regularly commit to the dependency as part of the same work? If yes, a submodule is usually easier, because the dependency is a real repository with its own branches and remotes.
- Do you need the parent to name an exact upstream commit and to show upstream changes as a single clear pointer update? If yes, use a submodule.
- Do you modify the dependency locally and rarely contribute back? If yes, use a subtree.
If the answers conflict, favor the preference of the people who will run the commands most often. A technically better tool that your team handles badly causes more damage than a slightly clumsy tool they understand.
Worked example: two projects, two choices
Project A is a web application that uses an internal design-system repository. A separate team releases it every two weeks. Several people on the application team never touch it, and the design-system repository has years of history and a large asset folder. Developers occasionally patch the design system in a branch and open a pull request against it.
This points to a submodule. The dependency has separate ownership, is large, and is edited as its own repository. The parent records which release is in use.
git submodule add https://github.com/example/design-system.git vendor/design-system
git config -f .gitmodules submodule.vendor/design-system.branch main
git commit -m "Add design-system as a submodule"Project B is an open source command-line tool that bundles a small parsing library, about 40 files. The maintainers carry two local patches and update the library a few times a year. Many contributors submit one-time pull requests, and the project must build from a source archive.
This points to a subtree. Contributors do not need new commands, the files are in the archive, and the patches are ordinary commits.
git subtree add --prefix=vendor/parser https://github.com/example/parser.git main --squash
git subtree pull --prefix=vendor/parser https://github.com/example/parser.git main --squashThe second command is run by a maintainer a few times a year.
Other options
Neither tool is always the right one. Before choosing, check whether a simpler route exists.
- Package manager. If the dependency is published to a registry such as npm, PyPI, Maven Central or crates.io, declare it in your manifest and lock file. This gives version pinning without embedding anything. Embedding makes sense mainly when no package exists, or when you must patch the source.
- Copying files once. If you will never sync with upstream, a plain copy plus a note about its origin is the simplest approach. You lose the ability to merge upstream changes, so use this only for code you will own from then on.
- Monorepo. If both pieces of code are yours and change together, moving them into one repository removes the need to embed anything.
Switching later
A choice is not permanent, but moving costs effort.
- Submodule to subtree: remove the submodule as described in the removal chapter (
deinit,git rm, delete.git/modules/...), then rungit subtree addinto the same path. Commit the removal first so the history is clear. - Subtree to submodule:
git rm -r vendor/libfoo, commit, thengit submodule addat the same path. The earlier vendored history stays in your log, which is fine.
Plan this as a separate, reviewed change. Tell the team beforehand, because every collaborator must update their working copy and, for submodules, run the initialization step.
Exercise
Your team of eight builds a mobile app. It depends on a 600 MB shared assets repository owned by another team, which tags a release every month. Only two of your developers ever need the assets locally, and CI runs on a service that you control and can configure. Which tool do you choose, and give two reasons from the decision procedure?
Show answer
Use a submodule. The dependency is large and not needed by everyone (question 3), so a subtree would add 600 MB to every clone and, without squashing, a long history too. It is also owned and released by another team, so a pointer to an exact released commit (question 5) matches how you consume it. Because you control CI, the usual drawback of submodules, needing extra fetch steps, is easy to handle with git submodule update --init --recursive. Developers who do not need the assets can skip initializing the submodule.
Exercise
A colleague committed a change inside vendor/libfoo (a submodule), committed the updated pointer in the parent, and pushed only the parent. Another developer now gets an error when running git submodule update. What went wrong, and which push option would have caught it?
Show answer
The parent now references a commit that exists only in the colleague's local submodule clone, so other machines cannot fetch it. The colleague should have pushed the submodule first. Running git push --recurse-submodules=check in the parent makes the push fail when a referenced submodule commit has not been pushed, and --recurse-submodules=on-demand pushes the submodule commits automatically before pushing the parent.
Exercise
You use a subtree with git subtree add --squash. Six months later a teammate runs git subtree pull for the same prefix without --squash. What risk does this create, and what is the best practice?
Show answer
Mixing squashed and unsquashed operations on the same prefix can produce confusing merges, conflicts, or duplicated upstream history, because Git subtree tracks the relationship using different commits in each mode. The best practice is to choose one mode when adding the subtree and use it for every later pull and push, and to write that rule in the project's contributor documentation.