Sub topic 11
Pushing changes back and splitting out a subtree
This chapter assumes you know everyday Git (commit, branch, merge, push, pull) and that you have already added a subtree with git subtree add and pulled upstream changes into it. Here you will send local edits back to the upstream repository and extract a directory's history into its own branch.
What a subtree push does
A subtree is a directory in your repository that came from another project. Its files are ordinary files in your history. If you fix a bug inside vendor/libfoo, that fix is a normal commit in your repository, mixed with commits that touch other paths.
git subtree push takes the commits that touched the prefix and rebuilds them as a history that fits the upstream repository. It then pushes that history to a branch you name.
git subtree push --prefix=vendor/libfoo https://github.com/example/libfoo.git my-fixThe three parts of the command are:
--prefix=vendor/libfoois the directory that holds the subtree.- The URL is the upstream repository to push to.
my-fixis the branch on the upstream repository that receives the commits.
Push to a new branch, not to main. The upstream maintainers can then review the branch and open a pull request.
How the history is rebuilt
Git walks your history and keeps only the commits that changed files under the prefix. It rewrites each one so that vendor/libfoo/parser.c becomes parser.c at the repository root. Commits that touched only other directories are dropped.
The result is a synthetic history. Its commit hashes differ from the ones in your repository, because the trees and parents are different. Running the command again reuses earlier work, so repeated pushes are consistent.
A typical workflow
- Edit files under
vendor/libfooand commit them in your repository. - Run
git subtree pushto a new upstream branch. - Open a pull request on the upstream project.
- After it is merged, run
git subtree pullto bring the merged result back.
git add vendor/libfoo/parser.c
git commit -m "Fix parser edge case"
git subtree push --prefix=vendor/libfoo https://github.com/example/libfoo.git my-fixThe final step is a pull with the same prefix and URL you used when you added the subtree:
git subtree pull --prefix=vendor/libfoo https://github.com/example/libfoo.git main --squashSplitting out a directory
git subtree split does the history rewrite from the push, but keeps the result local. It creates a branch that contains only the history of the prefix.
git subtree split --prefix=vendor/libfoo -b libfoo-onlyThe command prints the hash of the new branch tip. You can inspect it like any other branch:
git log --oneline libfoo-only
git ls-tree --name-only libfoo-onlySplitting is useful when:
- You want to inspect what
git subtree pushwould send before sending it. - A directory in your own project has grown into a library, and you want to publish it as a separate repository.
To publish a split branch as a new repository, push it to an empty remote:
git push https://github.com/example/new-lib.git libfoo-only:mainCommon errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `Updates were rejected` on push | The upstream branch has commits you do not have | Push to a new branch name, or pull first |
| `prefix does not exist` | Wrong path, or the command was run outside the repository root | Check the prefix spelling and run from the top level |
| Push is slow on a large history | Git must walk the whole history to rebuild it | Let it finish once. Later runs are faster |
| Upstream receives odd commit messages | Squashed pulls were included in the history | Use the same `--squash` choice consistently |
Exercise
You committed a fix under vendor/libfoo and ran git subtree push --prefix=vendor/libfoo <url> main. The push was rejected. What should you do, and what command would you use to check what would be sent first?
Show answer
The upstream main has moved or is protected, so push to a new branch such as my-fix and open a pull request. To check first, run git subtree split --prefix=vendor/libfoo -b libfoo-only and inspect it with git log --oneline libfoo-only.
Exercise
Why does the commit hash of a fix in your repository differ from the hash of the same fix upstream after a subtree push?
Show answer
Git rewrites the commit so paths no longer include the prefix. That changes the tree, and the parent commits are also rewritten, so the hash changes.