Sub topic 9
How subtrees work
This chapter assumes you are comfortable with commits, merges, and remotes, and that you know a commit hash identifies a snapshot and its history. You do not need the earlier submodule chapters, but they make the comparison at the end of this chapter easier to follow.
The core idea: files, not a pointer
A submodule stores a pointer to a commit in another repository. A subtree stores the other project's content itself. After you add a subtree, the files under vendor/libfoo are ordinary tracked files in your repository. They are in your tree objects and in your commits, and they travel with every clone and fork.
This has direct consequences:
- A plain
git clonegets everything. There is no.gitmodulesfile, no gitlink entry, and no initialization step. - Collaborators do not need to know that
vendor/libfoocame from somewhere else. - Your repository grows by the size of the vendored code, and by its history if you do not squash it.
- The link back to the upstream project is recorded only in commit messages, not in a configuration file.
What git subtree does
git subtree is a command that wraps a few merge techniques. It is not a new storage format. It runs on top of normal Git objects. Most Git installations include it, but some Linux distributions package it separately, so check that git subtree runs before you rely on it.
When you add a subtree, Git fetches the upstream branch and merges it into your history under a directory prefix. A prefix is the path inside your repository where the upstream root is placed. The upstream file README.md becomes vendor/libfoo/README.md.
You can choose how much upstream history to bring in:
| Mode | What your history contains | Trade-off |
|---|---|---|
| Full history (no `--squash`) | Every upstream commit, merged into your repository | Easy to run `git log` and `git blame` across the boundary, but the repository grows and the log fills with upstream commits |
| Squashed (`--squash`) | One commit per add or pull that holds the upstream snapshot | Small and tidy, but upstream commit-level history is not available locally |
Use the same mode for every add and pull on a given prefix. Mixing them tends to produce confusing merges.
How Git remembers the upstream
Git has no pointer to follow, so git subtree leaves markers in the commit messages it creates. With --squash, the squashed commit message includes trailer lines like these:
git-subtree-dir: vendor/libfoo
git-subtree-split: 4f2a9c1d0e8b7a6c5d4e3f2a1b0c9d8e7f6a5b4cgit-subtree-dir names the prefix. git-subtree-split holds the upstream commit that was imported. On the next pull or push, Git searches your history for these trailers. It uses them to find the last upstream commit it knows about, so it merges only what is new.
You can see this in your own repository after adding a subtree:
git log --oneline --graph -n 5
git ls-files vendor/libfoo | head
git log -n 1 --grep='git-subtree-dir' --format=%BThe second command lists the vendored files as normal tracked paths. The third prints the most recent message that contains the markers.
The data flow
The diagram shows how content moves between the upstream repository and your repository. The next chapter covers the commands that perform each arrow.
Editing vendored code
Because the files are normal, you can edit vendor/libfoo in the same commit as changes to your own code. Nothing blocks you and no extra steps are needed. This is the main convenience compared with submodules.
The cost is that upstream and local edits can collide. When you later pull a new upstream version, Git performs a regular merge. If you changed the same lines upstream changed, you resolve conflicts the way you would for any merge. Keep local patches to vendored code small and commit them separately from other work, so you can find them later.
When the model fits
Subtrees suit a project when:
- Contributors should clone and build with no extra commands.
- You rarely send changes back to the upstream project.
- You want the exact dependency code available even if the upstream repository disappears.
They fit less well when the vendored project is very large, or when you contribute to it often and need clean separation between its history and yours.
Exercise
A teammate runs a plain git clone of a repository that uses a subtree at vendor/libfoo. What will they find in that directory, and why does Git need the git-subtree-split line in a commit message later?
Show answer
They will find the full set of vendored files, because a subtree stores the content as ordinary tracked files in your repository, so no initialization step is needed. The git-subtree-split line records the upstream commit that was last imported. On a later git subtree pull or push, Git reads it to work out which upstream changes are new.