Subtrees: Including Another Project's History
Include another repository's files and history inside a folder of yours, with nothing extra for people who clone: git subtree add, pull, push and split, squashed or full history, the subtree merge underneath it in plain Git, and how subtrees compare with submodules.
kvi5hnuCode Sprout team member6 min readOn this page
A subtree puts another project's files into a folder of your repository, as ordinary files and commits. Unlike a submodule (article 41), nothing points elsewhere: anyone who clones your project gets everything, with no extra commands. Updates from the other project come in as merges, and you can even send your changes to that folder back to it. This article covers the git subtree command and the plain Git merge it's built on.
How a subtree works
A subtree is a merge of another repository's history, with its files moved into a subfolder. After that first merge, Git's subtree merge strategy knows that the other project's root corresponds to your folder, so later merges from it land in the right place. Your history contains the other project's commits, or a single squashed commit per update if you prefer.
git subtree
git subtree is a helper command shipped in Git's contrib folder. It's included in many installations, such as Git for Windows and Homebrew's Git. On some Linux distributions it comes in a separate package, or must be enabled from Git's documentation folder. Check with git subtree -h.
# add the library's main branch under libs/maps, as one squashed commit
git subtree add --prefix=libs/maps https://github.com/acme/maps-lib.git main --squash
# later: bring in its new commits
git subtree pull --prefix=libs/maps https://github.com/acme/maps-lib.git main --squash
# send your commits that changed libs/maps back to the library, as a branch
git subtree push --prefix=libs/maps https://github.com/acme/maps-lib.git from-trail-guide
# extract the folder's history into a separate branch, for a new repository
git subtree split --prefix=libs/maps -b maps-only
--prefixis the folder, relative to the top of the repository.--squashadds the library's state as one commit peraddorpull, instead of its entire history. It keeps your log short. Use it consistently: once you add with--squash, pull with--squashtoo.pushandsplitfind the commits in your history that touched the folder and rewrite them as commits of the library alone.pushsends them to a branch on the library's repository, where they can become a pull request.- A remote helps:
git remote add maps-lib <url>, thengit subtree pull --prefix=libs/maps maps-lib main --squash.
The same thing in plain Git
Without git subtree, the same result takes a few core commands, and doing it once shows what a subtree really is:
git init -q --bare maps-lib.git
git clone -q maps-lib.git maps-lib 2>/dev/null
(cd maps-lib && echo "export const version = 1;" > map.js && git add . && git commit -q -m "Map library v1" && git push -q origin main)
git init -q trail-guide && cd trail-guide
echo "# Trail guide" > README.md; git add .; git commit -q -m "Start project"
git remote add maps ../maps-lib.git
git fetch -q maps
# 1. start a merge that keeps our files, 2. read their tree into libs/maps/, 3. commit
git merge -s ours --no-commit --allow-unrelated-histories maps/main > /dev/null 2>&1
git read-tree --prefix=libs/maps/ -u maps/main
git commit -q -m "Add maps library under libs/maps"
ls libs/maps
git log --oneline --graph
echo "=== the library releases v2"
(cd ../maps-lib && echo "export const version = 2;" > map.js && git commit -q -am "Map library v2" && git push -q)
git fetch -q maps
git merge -q -Xsubtree=libs/maps maps/main -m "Update maps library"
cat libs/maps/map.js
git log --oneline --graph -4
map.js
* 9b97e2d Add maps library under libs/maps
|\
| * f26c469 Map library v1
* 7fb62a0 Start project
=== the library releases v2
export const version = 2;
* 0468023 Update maps library
|\
| * b66c102 Map library v2
* | 9b97e2d Add maps library under libs/maps
|\|
| * f26c469 Map library v1
git merge -s ours --no-commit --allow-unrelated-historiesrecords the library's history as merged, without taking any of its files at the top level.git read-tree --prefix=libs/maps/ -u maps/mainplaces the library's files inlibs/maps/, staged and in the working tree. The commit completes the merge.- Later updates are ordinary merges with
-Xsubtree=libs/maps, which tells the merge where the library's files live in your tree.
The library's commits ("Map library v1", "v2") are now part of your history. git log -- libs/maps shows the merges that brought them in, and git log maps/main the library's own history.
Working with subtree code
- Edit the files freely. They're ordinary files in your repository. Keep changes to the subtree folder in separate commits from changes elsewhere, so
git subtree pushorsplitcan extract them cleanly. - Contributing back is optional:
git subtree pushsends your changes as a branch to the library's repository. Without it, the changes stay yours, which is fine for a vendored copy you maintain. - Conflicts during
subtree pullhappen when both you and the library changed the same lines. Resolve them like any merge (article 13).
Subtree or submodule?
| Submodule | Subtree | |
|---|---|---|
| What your repository stores | a pointer to a commit of the other repository | the other project's files, and its history or a squashed copy |
| Cloning | needs --recurse-submodules or submodule update --init | nothing special |
| Updating | git submodule update --remote, then commit the gitlink | git subtree pull (a merge) |
| Changing the included code | commit and push inside the submodule first | edit normally. Optionally subtree push back |
| Repository size | small, as the content stays separate | grows with the included project |
| Everyone needs to know the commands | yes | mostly only whoever updates it |
Choose submodules when the other project is large, changes often, or must stay strictly separate. Choose subtrees when you want a simple experience for everyone who clones, and update the included code occasionally. For published libraries, a package manager is usually better than either.
Extracting a folder into its own repository
git subtree split also works in the opposite direction: to turn a folder of a large repository into a new project, with its history:
git subtree split --prefix=libs/maps -b maps-only
git push git@github.com:acme/maps-lib.git maps-only:main
git filter-repo --subdirectory-filter libs/maps does the same on a copy of the whole repository, and is faster on large histories (article 47).
What's current
git subtreeremains in Git'scontribarea, maintained and shipped with most installations. Git 2.51 taught it to sign the commits it creates when you use GPG signing.- The underlying subtree merge (
-Xsubtree=<path>) is part of core Git and works with the defaultortmerge strategy.
Common questions
Do people who clone my project need to do anything for a subtree?
No. The files are ordinary files in your repository, and git clone gets them all. Only someone updating the subtree from its source needs the git subtree command or the merge steps.
Should I use --squash?
Usually yes, for third-party code you don't contribute to: your history gets one commit per update instead of the library's whole history. Without --squash, you keep the full history, which helps if you push changes back often.
git subtree says "is not a git command". What now?
Your Git installation doesn't include the contrib command. Install your distribution's package that provides it, or use the plain Git steps shown above.
Can I convert a submodule into a subtree?
Yes. Remove the submodule (git rm <path>, commit), then git subtree add --prefix=<path> <url> <commit-or-branch> at the same commit.
Summary
- A subtree includes another project's files, and its history or a squashed copy, in a folder of your repository. Clones need nothing extra.
git subtree add,pull,pushandsplitmanage it. Use--prefixfor the folder, and--squashconsistently for a short history.- Underneath, it's a merge:
merge -s ourswithread-tree --prefixto add, andmerge -Xsubtree=<path>to update. - Keep commits to the subtree folder separate, so they can be pushed back or split out.
- Submodules keep projects separate with a pointer. Subtrees copy them in for simplicity.
Practice questions
Add the main branch of https://github.com/acme/ui-kit.git under vendor/ui-kit as a single commit, then update it later.
Answer
git subtree add --prefix=vendor/ui-kit https://github.com/acme/ui-kit.git main --squash
# later
git subtree pull --prefix=vendor/ui-kit https://github.com/acme/ui-kit.git main --squash
A remote (git remote add ui-kit <url>) saves typing the URL each time.
You fixed a bug in vendor/ui-kit in your project, and the library's maintainers want the fix. How do you send it?
Answer
git subtree push --prefix=vendor/ui-kit https://github.com/acme/ui-kit.git fix-from-app extracts your commits that touched the folder, rewrites them as library commits, and pushes them to a branch, from which you open a pull request. It works best when those commits changed only files under vendor/ui-kit.
Why does a subtree make cloning simpler than a submodule?
Answer
With a subtree, the other project's files are stored in your repository like any others, so a normal git clone gets everything. A submodule stores only a pointer to a commit in another repository, so every clone must also run git submodule update --init or clone with --recurse-submodules.
Previous: Submodules: Repositories Inside Repositories
Next: Large Files: Git LFS and Keeping Binaries Out of History

More in Git Course
Quiz: Git Delicate Details
Twenty-five questions on Git's subtle corners: what commit -a and stash leave out, empty folders, ignore patterns and negation, HEAD~ versus HEAD^, ours and theirs in a rebase, tags that don't move or don't push, describe, merge reverts, diff exit codes, stash conflicts and more.
Quiz: Git Interview Questions
Thirty Git questions of the kind asked in developer interviews: Git versus GitHub, the staging area, merge versus rebase, fetch versus pull, reset, revert and restore, detached HEAD, conflicts, force pushing, stash, cherry-pick, reflog, bisect, submodules, LFS and how commits are identified.
Quiz: Git Course Final Exam (All Parts)
The final exam for the Git course: thirty questions across all twelve parts, from the three areas and staging through branching, remotes, undoing, history investigation, configuration, tags, larger projects, patches, rewriting, internals and troubleshooting.
Quiz: Professional Git Practice (Git Part 12)
Sixteen questions on migrating to Git from Subversion, TFVC and Mercurial, team best practices for commits, branches and reviews, repository hygiene, and diagnosing and fixing the most common Git errors, with explanations.
Discussion
Sign in to ask a question or share what you learned.