Sub topic 10
Adding a subtree and pulling upstream changes
This chapter covers the two commands you run most often with subtrees: git subtree add to bring in another project, and git subtree pull to take in its newer work. It assumes you can read git log --graph output and resolve a merge conflict. It also assumes you know the subtree model from the previous chapter: the upstream files become ordinary files in your repository, with no pointer file and no extra clone to manage.
Before you start
git subtree ships in Git's contrib area and is included in most installers and Linux packages. Check that you have it:
git subtree -hIf that prints a usage message, you are ready. Two requirements apply to every git subtree command in this chapter:
- Your working tree must be clean. Commit or stash first. Otherwise the command stops with a message such as
Working tree has modifications. Cannot add. - Run the command from the top level of the repository. The
--prefixpath is relative to that top level.
Adding a subtree
The basic form needs a prefix, a repository URL, and a ref (a branch, tag, or commit) to import:
git subtree add --prefix=vendor/libfoo https://github.com/example/libfoo.git main --squashThe pieces mean this:
| Part | Meaning |
|---|---|
| `--prefix=vendor/libfoo` | The directory that will hold the imported files. It must not already exist. |
| `https://github.com/example/libfoo.git` | The upstream repository. A named remote also works. |
| `main` | The upstream ref to import. A tag such as `v1.2.0` works too. |
| `--squash` | Collapse the upstream history into one commit instead of importing every commit. |
Git fetches the upstream ref, then creates the files under vendor/libfoo. Afterward the files are tracked like any other file in your repository. Anyone who clones your repository gets them with no extra step.
What the command creates
With --squash, git subtree add makes two commits. Run git log --graph --oneline to see them:
git log --graph --oneline -n 4The output looks like this:
* 3f2a1b9 Merge commit 'a81c4de...' as 'vendor/libfoo'
|\
| * a81c4de Squashed 'vendor/libfoo/' content from commit 9d07e12
* 5c1e7a0 Previous work in your projectThe squash commit holds the upstream content as a single snapshot. The merge commit joins that snapshot into your branch under the prefix. The squash commit's message includes two trailer lines that git subtree reads later:
git-subtree-dir: vendor/libfoo
git-subtree-split: 9d07e12...git-subtree-dir records the prefix. git-subtree-split records the exact upstream commit that was imported. These trailers are how Git knows where the previous import stopped, so don't rewrite or edit those commits.
Squash or full history
Without --squash, Git imports every upstream commit into your history. Your git log then contains the library's whole past, and git blame can see through to the original authors.
With --squash, your history stays short. Each import or update adds one commit, and the upstream history stays in the upstream repository.
For vendored code you don't intend to dig through, --squash is the usual choice. Its cost is that tools such as git blame stop at the squash commit instead of showing the original upstream commits.
Using a named remote
Typing the URL every time is error-prone. You can register the upstream as a remote and refer to it by name:
git remote add -f libfoo https://github.com/example/libfoo.git
git subtree add --prefix=vendor/libfoo libfoo main --squashThe -f flag fetches right away. The remote is stored in your local .git/config only, so it is not shared. Teammates who clone the repository won't have it unless they add it themselves. Because subtree history lives in your normal commits, only people who update the subtree need the remote.
Pulling upstream changes
When upstream publishes new work, use the same prefix and the same ref style:
git subtree pull --prefix=vendor/libfoo https://github.com/example/libfoo.git main --squashInternally this runs a fetch of the ref, then a merge with the subtree merge strategy. That strategy matches the paths in the upstream tree against the files under your prefix, so upstream's src/parser.c is merged with your vendor/libfoo/src/parser.c.
With --squash, the pull creates a new squash commit for the upstream changes since the last import, followed by a merge commit into your branch. Here is how the history looks after an add and one pull:
The dotted line shows that the second squash commit is built relative to the first. Git finds the earlier import through the git-subtree-split trailer, so it knows which upstream commit was already taken.
Pulling a tagged release
If upstream marks releases with tags, pull a tag instead of a moving branch. Your vendored copy then matches a release rather than whatever was on main that day:
git subtree pull --prefix=vendor/libfoo https://github.com/example/libfoo.git v1.1.0 --squashThis is a good habit for production dependencies. The commit message records the tag's commit, and you can later see which release you run.
Finding the current version
The subtree has no .gitmodules file or pointer to read. To find the upstream commit you last imported, search the trailers:
git log --grep="git-subtree-dir: vendor/libfoo" -n 1 --format="%H %b"The git-subtree-split: line in the output is the upstream commit. If you imported without --squash, the same trailers appear in the merge commit's message instead.
Local edits and conflicts
Because the files are yours, you can edit them. Say you patched vendor/libfoo/src/parser.c and committed it in your repository. On the next pull, Git three-way merges your change with upstream's change, exactly as for a normal branch merge.
If both sides touched the same lines, the pull stops with a conflict:
Auto-merging vendor/libfoo/src/parser.c
CONFLICT (content): Merge conflict in vendor/libfoo/src/parser.c
Automatic merge failed; fix conflicts and then commit the result.Resolve it the usual way:
git status
# edit vendor/libfoo/src/parser.c and remove the conflict markers
git add vendor/libfoo/src/parser.c
git commitGit leaves the merge message ready for you, so you can accept it as is. Keep local edits small and well labeled in your commit messages. Each one is a possible conflict on a later pull. If you expect to change the library heavily, the next chapter's push workflow lets you send those changes upstream so they stop diverging.
Common errors and fixes
The following problems account for most trouble with add and pull.
Mixing squash and non-squash
Choose --squash or not at the first add, and use the same choice on every pull. If you add with --squash and later pull without it, Git merges the full upstream history against a prefix whose history it never saw. The result is often a large, confusing merge with spurious conflicts. If this happens, abort with git merge --abort (if the merge is unfinished) or git reset --hard ORIG_HEAD (if it finished and you haven't pushed), then rerun the pull with --squash.
Note that git reset --hard discards uncommitted work, so check git status first.
Wrong or missing prefix
The prefix on pull must match the one used on add, with no trailing slash. A mismatch, such as vendor/libfoo/, can make Git complain that it can't find a previous subtree import. A typo can also make it fail with can't squash-merge: 'vendor/libfoo' was never added. Check the exact string in the git-subtree-dir trailer.
Prefix already exists
git subtree add refuses to run if the directory exists:
prefix 'vendor/libfoo' already exists.Move or remove the old directory first, commit that change, and then add the subtree. If the old directory contains files you want to keep, copy them elsewhere. Don't add a subtree on top of existing files, because Git would have no shared history to merge against.
Pulling with a dirty tree
A Working tree has modifications error means uncommitted changes are present. Run git stash or commit, then pull again.
Shallow clones
A shallow clone of your own repository (git clone --depth 1) may lack the commits git subtree needs to locate the previous import. If a pull fails with a message about missing commits, run git fetch --unshallow and try again.
A complete example
This sequence imports a library, patches it locally, and later pulls a new release:
# Start from a clean tree on your main branch
git switch main
git status
# 1. Import release v1.0.0
git subtree add --prefix=vendor/libfoo https://github.com/example/libfoo.git v1.0.0 --squash
# 2. Make a local change inside the prefix
echo "/* local patch */" >> vendor/libfoo/src/parser.c
git commit -am "Local patch to libfoo parser"
# 3. Later, pull release v1.1.0
git subtree pull --prefix=vendor/libfoo https://github.com/example/libfoo.git v1.1.0 --squash
# 4. Confirm what was recorded
git log --graph --oneline -n 6After step 3, your patch and the upstream changes sit together under vendor/libfoo. If the patch touched lines upstream also changed, expect the conflict flow from the previous section.
Exercise
You added libfoo with git subtree add --prefix=vendor/libfoo <url> main --squash. Three months later a teammate runs git subtree pull --prefix=vendor/libfoo <url> main without --squash and gets a large merge with many conflicts. What went wrong, and how do you recover and avoid it next time?
Show answer
The subtree was added with --squash, so your history contains only one squashed snapshot of libfoo, and the pull without --squash tried to merge upstream's full commit history against it. Git had no shared ancestor for those commits, which produced the spurious conflicts. To recover, run git merge --abort if the merge is unfinished, or git reset --hard ORIG_HEAD if it was committed but not pushed, after confirming with git status that you have no uncommitted work. Then rerun the pull with --squash. To avoid it, use the same flag on every add and pull, ideally from a script or Makefile target.
Exercise
Which command shows the upstream commit that your subtree at vendor/libfoo was last synchronized to, and where does Git store that information?
Show answer
Run git log --grep="git-subtree-dir: vendor/libfoo" -n 1 --format="%H %b". Git stores the information in trailer lines in the squash commit message: git-subtree-dir holds the prefix and git-subtree-split holds the upstream commit hash. Without --squash, the same trailers are in the merge commit message.