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 an exact snapshot, and have run the submodule and subtree commands from the earlier chapters. Here you compare the two approaches and pick one for a given team.
What each approach stores
A submodule stores a pointer. The parent repository records a gitlink, which is the hash of one commit in the other repository. The .gitmodules file records the path and URL. The files of the other project are not part of the parent's history.
A subtree stores the files themselves. After git subtree add, the code under vendor/libfoo is ordinary content in your repository. With --squash, upstream history is collapsed into a single commit. Without it, upstream history is merged into yours.
Comparing day-to-day behavior
| Concern | Submodule | Subtree |
|---|---|---|
| Cloning | Needs `--recurse-submodules` or `git submodule update --init --recursive` | A plain `git clone` is enough |
| Repository size | Small. Only a pointer is stored | Larger. Upstream files, and possibly history, are stored |
| Pinning a version | Exact commit, always visible in the parent | The merged content is the version, but the upstream commit is less obvious |
| Updating | `git submodule update --remote`, then commit the new pointer | `git subtree pull`, which creates a merge or squash commit |
| Changing the embedded code | Commit and push in the submodule first, then in the parent | Edit like any other file. Optionally `git subtree push` upstream |
| Required knowledge | Every contributor must know the submodule workflow | Contributors can ignore that the directory came from elsewhere |
| Removal | Several steps (`deinit`, `git rm`, delete `.git/modules`) | `git rm -r` and commit |
| Offline use | Needs the submodule remote to be reachable on first clone | Fully self-contained |
Failure modes to weigh
Submodules fail in predictable ways:
- A teammate pushes the parent before the submodule commit. Others then fetch a pointer to a commit that does not exist on the remote.
git push --recurse-submodules=checkguards against this. - A plain
git cloneorgit pullleaves submodule directories empty or at the wrong commit. - Work done in a detached HEAD inside a submodule can be lost if you don't create a branch first.
Subtrees fail differently:
- The repository grows, and without
--squashthe log fills with upstream commits. - Mixing upstream-bound edits with local-only edits in the same directory makes
git subtree pushharder to reason about. - Using
--squashon some commands and not others for the same prefix can cause confusing merges. Pick one convention and keep it. - It is easy to forget that the code is a copy. Edits made locally can conflict with the next
git subtree pull.
Decision criteria
Use these questions in order.
- Does your team need an exact, auditable version of the dependency, and does it often work on the dependency at the same time? Submodules fit, because the pointer is explicit and the dependency keeps its own history and remote.
- Do you want contributors to use only plain Git commands? Subtrees fit, because nothing special happens on clone, pull, or checkout.
- Is the dependency large, with long history? A submodule keeps the parent small. A squashed subtree reduces the cost but still copies the files.
- Do you rarely modify the dependency and mostly consume releases? Either works. Subtrees are simpler for readers, and submodules are cleaner for tracking versions.
- Do your build or CI systems clone without extra flags? Subtrees avoid the extra setup. Submodules need that setup added.
Worked example
A web team vendors a small parser library that it patches often, and the library's maintainers accept patches. Three developers work on both projects daily and are comfortable with submodules. A submodule works well here, because they can commit in vendor/libfoo, push a branch, and then update the pointer.
A different team copies in a stable logging helper twice a year and has many occasional contributors. A squashed subtree works well, because nobody needs to learn a new workflow, and a plain clone produces a working build.
Exercise
Your CI system runs a plain git clone and then builds. The project embeds a library that nobody on the team edits, and you want the fewest CI changes. Which approach fits better, and what is one cost of choosing it?
Show answer
A subtree fits better, because a plain clone already contains the library files and the build works without extra flags. The cost is a larger repository, and local edits to the copied directory can conflict with later git subtree pull runs. A submodule would instead require --recurse-submodules or git submodule update --init --recursive in CI.
Exercise
A teammate commits a changed submodule pointer in the parent and pushes the parent, but forgets to push the commit inside the submodule. What happens for others, and which command helps prevent it?
Show answer
Others who pull the parent get a pointer to a commit that does not exist on the submodule's remote, so git submodule update fails to check it out. Running git push --recurse-submodules=check makes the parent push fail until the submodule commit has been pushed.