Git Subtree
git subtree is a command that copies the contents of another repository into a folder of your repository, as ordinary files in ordinary commits. Unlike a submodule, the files really live in your project: anyone who clones it gets them, with no extra steps. git subtree also remembers where the folder came from, so you can later pull in new versions from the other repository, or send your changes to the folder back to it. It is used to include shared code that should be part of the project for everyone, such as a library the team occasionally edits.
If a submodule is a recipe that says "see page 12 of the sauce book", a subtree is a recipe book with the sauce recipe printed inside it, plus a note saying which edition it was copied from, so that you can copy a newer edition later.
Syntax
git subtree add --prefix=<folder> <repository> <branch> [--squash]
git subtree pull --prefix=<folder> <repository> <branch> [--squash]
git subtree push --prefix=<folder> <repository> <branch>
git subtree split --prefix=<folder> [-b <new-branch>]| Command | Description |
|---|---|
git subtree add | It is used to copy a branch of another repository into a new folder, as a merge commit. |
git subtree pull | It is used to bring newer commits from that repository into the folder. |
git subtree push | It is used to send commits that changed the folder back to the other repository. |
git subtree split | It is used to create a branch whose history contains only the folder, as if it had always been its own repository. |
--squash | It is used to bring in the other history as one combined commit, instead of every commit. |
git subtree is a script from Git's contrib folder. Git for Windows and most macOS and Linux packages include it; if git subtree says it is not a Git command, install your distribution's extra Git package.
The Starting Point
As in "Git Submodules", the school's fee rules have their own repository, and a bare fee-rules.git stands in for its server:
git init --quiet fee-rules
cd fee-rules
echo "Late fee after day: 10" > rules.txt
git add rules.txt
git commit --quiet --author="Srihitha Anamaneni <srihitha@example.com>" -m "Add the late fee rule"
echo "Sibling discount: 10%" >> rules.txt
git commit --quiet -am "Add the sibling discount rule" --author="Srihitha Anamaneni <srihitha@example.com>"
cd ..
git clone --quiet --bare fee-rules fee-rules.git
git init --quiet fee-tracker
cd fee-tracker
echo 'Term 1 fee: $12,000' > fees.txt
git add fees.txt
git commit --quiet -m "Add the term fee" Example 1: Adding a Subtree
git subtree add --prefix=rules ../fee-rules.git main --squashgit fetch ../fee-rules.git main
From ../fee-rules
* branch main -> FETCH_HEAD
Added dir 'rules'Explanation: git subtree printed the fetch it runs, fetched main from fee-rules.git and added its files under rules/. With --squash, the library's two commits became one combined commit, so your history doesn't fill up with the library's history. Look at the result:
ls rules
git log --oneline --graph
git log -1 --format=%b HEAD^2rules.txt
* 10b3470 (HEAD -> main) Merge commit '0aee838e3c2eff2526a9b8cb3c225ee3bfe77c64' as 'rules'
|\
| * 0aee838 Squashed 'rules/' content from commit a29c607
* 28024e2 Add the term fee
git-subtree-dir: rules
git-subtree-split: a29c60727bfdb4657c14175aa56956bc485de5f4Explanation: the files are real files in the project. The history has a squash commit with the library's contents and a merge commit that put them in rules/. The squash commit's message ends with two lines, git-subtree-dir and git-subtree-split: they record the folder and the library commit it came from. git subtree pull reads them later to know what is new.
Anyone who clones the Fee Tracker now gets rules/ with no extra steps:
cd ..
git clone --quiet fee-tracker advitha
cat advitha/rules/rules.txtLate fee after day: 10
Sibling discount: 10%Example 2: Pulling a New Version
Srihitha adds a rule to the library and pushes it:
cd fee-rules
git remote add origin ../fee-rules.git
echo 'Hostel deposit: $2,000' >> rules.txt
git commit --quiet -am "Add the hostel deposit rule" --author="Srihitha Anamaneni <srihitha@example.com>"
git push --quiet origin main Navika brings it into the Fee Tracker:
cd ../fee-tracker
git subtree pull --prefix=rules ../fee-rules.git main --squash -m "Update the fee rules"From ../fee-rules
* branch main -> FETCH_HEAD
Merge made by the 'ort' strategy.
rules/rules.txt | 1 +
1 file changed, 1 insertion(+)tail -1 rules/rules.txt
git log --oneline --graphHostel deposit: $2,000
* 71c5855 (HEAD -> main) Update the fee rules
|\
| * 0cd8f89 Squashed 'rules/' changes from a29c607..4750beb
* | 10b3470 Merge commit '0aee838e3c2eff2526a9b8cb3c225ee3bfe77c64' as 'rules'
|\|
| * 0aee838 Squashed 'rules/' content from commit a29c607
* 28024e2 Add the term feeExplanation: git subtree pull fetched the new library commit, squashed it, and merged it into rules/. The project's history shows each update as a squash commit plus a merge.
Example 3: Sending a Change Back
Navika fixes a rule directly in the Fee Tracker, then sends the fix to the library:
sed -i.bak "s/day: 10/day: 15/" rules/rules.txt && rm rules/rules.txt.bak
git commit --quiet -am "Move the late fee to day 15"
git subtree push --prefix=rules ../fee-rules.git main 2>&1 | tail -1
git -C ../fee-rules.git log --oneline -2 4750beb..8aa4fc1 8aa4fc1dd33babe49f99639c00d93335e713b01b -> main
8aa4fc1 (HEAD -> main) Move the late fee to day 15
4750beb Add the hostel deposit ruleExplanation: git subtree push looked for commits that changed rules/, rewrote them as if rules/ were the top folder (a split), and pushed the result to the library's main. The library now has the fix as its own commit, with the same message. (The 2>&1 | tail -1 only keeps the last line of the push's progress messages.) Keep commits that change the subtree separate from other changes, so that their messages make sense in the library too.
Example 4: Splitting a Folder into Its Own Repository
git subtree split also works the other way: it can turn a folder that grew inside a project into the start of a separate repository, keeping the folder's history:
git subtree split --prefix=rules -b rules-only
git log --oneline rules-only
git ls-tree --name-only rules-onlyCreated branch 'rules-only'
8aa4fc1dd33babe49f99639c00d93335e713b01b
8aa4fc1 (rules-only) Move the late fee to day 15
4750beb Add the hostel deposit rule
a29c607 Add the sibling discount rule
494241e Add the late fee rule
rules.txtExplanation: split created the branch rules-only and printed the ID of its top commit. In it, the files of rules/ are at the top level, and only commits about the folder remain. Because rules/ came from the library, split followed the git-subtree-split lines back to the library's own commits, so rules-only is exactly the library's history, ending in 8aa4fc1, the same commit that git subtree push created. For a folder that grew inside the project, split builds that history from the project's own commits; push the branch to a new, empty repository, and the folder becomes a project of its own.
Submodule vs Subtree vs Package Manager
| Submodule | Subtree | Package manager (Maven, Gradle, npm) | |
|---|---|---|---|
| Files stored in your repository | No, only a pinned commit | Yes, as normal files | No, only a version number in pom.xml or package.json |
| Extra steps after cloning | git submodule update --init | None | The build downloads it |
| Editing the shared code | Inside the submodule, with its own commits and pushes | Edit as normal files, then git subtree push | In the library's own project, then publish a new version |
| Teammates must learn | Submodule commands | Nothing for daily work | The build tool, which they use anyway |
| Best for | Large or separately released code that you rarely change | Small shared code everyone needs, edited now and then | Libraries with versioned releases: the usual choice for Java |
Common Mistakes
Mixing subtree and project changes in one commit. git subtree push sends the whole commit message to the library. Commit subtree changes separately.
Forgetting --squash on pull after adding with it. Use the same choice every time, or git subtree may complain or bring in the whole library history.
Changing the squash commit messages. The git-subtree-dir and git-subtree-split lines are what git subtree uses to find the last update. Don't rewrite them.
Choosing a subtree for a library with releases. A package manager handles versions far better.
Under the Hood
git subtree is a shell script that uses ordinary Git commands. add fetches the other branch, optionally squashes its history into one commit (whose tree is the library's tree), and makes a merge commit using the subtree merge strategy, which places the incoming tree under the --prefix folder instead of at the top. pull does the same with the newer commits, finding the previous update through the git-subtree-split line. split walks your history, keeps only commits that changed the folder, and writes new commits whose trees are that folder's tree; because the same input always produces the same commits, repeated splits produce matching IDs, which is how push can send only what is new.
Interview Corner
What is the difference between a submodule and a subtree?
A submodule stores only a pinned commit of another repository, which each clone must fetch separately. A subtree copies the other repository's files into a folder of your project, so they are part of every clone, and git subtree can still pull updates and push changes back.
What does git subtree split do?
It creates a branch whose history contains only one folder, with the folder's files at the top level. It is used to push changes back to the original repository, or to turn a folder into a separate repository.
What does --squash do in git subtree add and pull?
It brings the other repository's commits in as one combined commit, so your history doesn't include every library commit.
For a Java project sharing a library, would you use a submodule, a subtree or Maven?
Maven or Gradle: publish the library with a version and depend on it. Submodules and subtrees suit unpackaged shared files, such as configuration or scripts.
Practice
Challenge: what came from where. Print the git-subtree-split line of every squash commit in the Fee Tracker's history, newest first.
What does Git print? Before you open the solution, decide how many lines there are and which library commits they name.
Show solution
git log --format=%b main | grep "git-subtree-split"
git -C ../fee-rules.git log --onelinegit-subtree-split: 4750beb4be397fc83494182987af73e16fb8ccef
git-subtree-split: a29c60727bfdb4657c14175aa56956bc485de5f4
8aa4fc1 (HEAD -> main) Move the late fee to day 15
4750beb Add the hostel deposit rule
a29c607 Add the sibling discount rule
494241e Add the late fee ruleTwo lines: one for each squash commit, from the first add and the pull. They name the library commits "Add the hostel deposit rule" and "Add the sibling discount rule", the library's tip at each update. Navika's pushed fix doesn't appear here, because push doesn't create a squash commit.
Checkpoint. Add a small public repository as a subtree of a practice project with --squash, clone the project again to confirm the files are there without extra steps, and run git subtree pull after the library changes.
Key Takeaways
Quick reference
| Command | It is used to |
|---|---|
git subtree add --prefix=<folder> <repo> <branch> --squash | Copy a repository into a folder |
git subtree pull --prefix=<folder> <repo> <branch> --squash | Bring in a newer version |
git subtree push --prefix=<folder> <repo> <branch> | Send folder changes back |
git subtree split --prefix=<folder> -b <branch> | Extract a folder's history |
Was this tutorial helpful?