Sub topic 10
Adding a subtree and pulling upstream changes
This chapter assumes you know everyday Git (commit, merge, remotes) and the subtree idea from the previous chapter. A subtree copies another project's files into a directory of your repository, and those files become ordinary tracked content. Here you add one and bring in later upstream changes.
Adding a subtree
git subtree add needs three things: a prefix (the directory to create), the upstream repository, and a branch or ref to copy.
git subtree add --prefix=vendor/libfoo https://github.com/example/libfoo.git main --squashGit fetches main from the URL and places its files under vendor/libfoo. It then records the result as commits in your current branch. Four rules apply:
- Run the command from the top level of your repository.
- Write the prefix without a trailing slash.
- The directory must not already exist.
- Your working tree must be clean.
--squash collapses the upstream history into one commit. Without it, every upstream commit is added to your history. Squashing keeps git log short, and you can still see the upstream commit it came from.
After the command, your history has two new commits. One holds the squashed upstream content. The other is a merge commit that places it under the prefix. Nothing else changes in the repository. There is no .gitmodules file and no gitlink. Teammates who clone your project get the files directly and need no extra steps.
What Git records
Git does not keep a separate configuration for the subtree. It writes metadata into the commit messages instead. The squash commit contains lines like these:
git-subtree-dir: vendor/libfoo
git-subtree-split: 3f2a9c1d5e7b8a406c1f2e9d0b7a5c3e1f4d6a82git-subtree-dir is the prefix. git-subtree-split is the upstream commit that was imported. When you pull later, Git searches your history for these lines to find the last imported commit.
Using a named remote
Typing the URL each time is tedious and easy to get wrong. Register a remote once and use its name instead.
git remote add libfoo https://github.com/example/libfoo.git
git subtree add --prefix=vendor/libfoo libfoo main --squashThe remote is stored only in your local clone. Teammates do not receive it, and they need it only if they pull or push the subtree themselves.
Pulling upstream changes
When upstream publishes new commits, bring them in with git subtree pull. Use the same prefix, repository, and branch.
git subtree pull --prefix=vendor/libfoo https://github.com/example/libfoo.git main --squashGit fetches the branch and finds the last imported commit from the recorded metadata. It then merges the difference into vendor/libfoo. The merge is an ordinary three-way merge. If you edited files in the directory and upstream changed the same lines, you get conflicts. Resolve them as you would in any merge, then run git commit.
flowchart LRThe diagram below shows how the commits fit together.
Common errors
| Message or symptom | Cause | Fix |
|---|---|---|
| `working tree has modifications. Cannot add.` | Uncommitted changes exist | Commit or stash first |
| `prefix 'vendor/libfoo' already exists.` | The directory is already present | Choose a new prefix, or use `pull` if it is already a subtree |
| `can't squash-merge: 'vendor/libfoo' was never added.` | No subtree metadata was found in history, or the prefix is misspelled | Check the prefix, and check that the original add was not rewritten by a rebase |
| Merge conflicts after `pull` | You and upstream edited the same lines | Resolve the conflicts, then commit |
A rebase can discard the merge commits that subtree add created. Avoid rebasing branches that contain subtree commits unless you know how it affects the metadata.
Checking the result
After a pull, git log --oneline -3 shows the merge and squash commits. To see which upstream commit you currently have, search the log for the metadata:
git log -1 --grep="git-subtree-dir: vendor/libfoo"The commit message printed by this command includes the git-subtree-split line.
Exercise
You ran git subtree add --prefix=vendor/libfoo libfoo main --squash last month. Upstream has new commits. Write the command that brings them in, and say how Git knows which upstream commit you last imported.
Show answer
Run git subtree pull --prefix=vendor/libfoo libfoo main --squash from the repository top level with a clean working tree. Git reads the git-subtree-dir and git-subtree-split lines in the earlier squash commit message, which give the prefix and the last imported upstream commit. It then merges the changes since that commit into vendor/libfoo.