Sub topic 11
Pushing Changes Back and Splitting Out a Subtree
You can already add a subtree and pull upstream changes into it. This chapter covers the other direction: sending your edits to the upstream repository, and extracting a directory's history into its own branch.
This chapter assumes you are comfortable with:
- Everyday Git (commit, branch, merge, push).
- Remotes and what a commit hash identifies.
- Adding a subtree with
git subtree addand pulling updates withgit subtree pull.
What a subtree push really does
A subtree is ordinary files inside your repository. Your commits that touch vendor/libfoo are mixed with commits that touch everything else. The upstream repository has none of your other files, and its paths start at its own root, not at vendor/libfoo/.
git subtree push therefore does two jobs:
- It runs the same logic as
git subtree split. It walks your history, keeps only the changes under the prefix, and rewrites each kept commit so the prefix is removed from the paths. - It pushes the resulting tip commit to a branch on the repository you name.
The rewritten commits keep the original author, dates, and messages. Commits that never touched the prefix are dropped. A commit that touched both the prefix and other files is kept, but only its prefix part survives.
The synthetic commits are deterministic. Running the split twice on the same history gives the same hashes. That property is what makes repeated pushes work: the second push builds on the commits from the first.
A runnable example
The script below builds a local "upstream" library and an application that embeds it. It needs no network. It assumes Git 2.28 or newer (for git init -b) and that user.name and user.email are set.
set -e
rm -rf demo && mkdir demo && cd demo
# The upstream library, with one commit
git init -b main libfoo-work
cd libfoo-work
echo "parse v1" > parser.txt
git add parser.txt
git commit -m "Initial libfoo"
cd ..
git clone --bare libfoo-work libfoo.git
# The application, which embeds the library as a subtree
git init -b main app
cd app
echo "app v1" > README.md
git add README.md
git commit -m "Initial app"
git subtree add --prefix=vendor/libfoo ../libfoo.git main --squash
# One commit that touches both the subtree and the app
echo "parse v1 with edge case fix" > vendor/libfoo/parser.txt
echo "app v2" > README.md
git commit -am "Fix parser edge case and update README"
# Send only the subtree part to a new upstream branch
git subtree push --prefix=vendor/libfoo ../libfoo.git my-fix
# Inspect what upstream received
git --git-dir=../libfoo.git log --oneline my-fix
git --git-dir=../libfoo.git ls-tree --name-only my-fixThe last two commands show the key behavior. The upstream branch my-fix contains the original "Initial libfoo" commit and your "Fix parser edge case and update README" commit. Its tree lists only parser.txt, at the root. The README change stayed behind, and the vendor/libfoo/ prefix is gone.
Your history builds on the upstream commit because the squash commit created by subtree add records which upstream commit it came from. Git reads those records to find where upstream history joins yours.
Push to a branch, not to main
Pushing to a new branch such as my-fix is the safest habit. The upstream maintainers can then review the work as a pull request. Pushing straight to main works only if you have write access and the push is a fast-forward.
If you push often, add a remote so you don't retype the URL:
git remote add libfoo ../libfoo.git
git subtree pull --prefix=vendor/libfoo libfoo main --squash
git subtree push --prefix=vendor/libfoo libfoo my-fixA remote name is only a shortcut. Git subtree does not store it anywhere or track it as a relationship. You still pass --prefix every time.
When the push is rejected
Subtree push is a normal Git push of the split commits, so it is rejected if the target branch has moved on in a way your split history doesn't contain. The usual fix is to pull first:
git subtree pull --prefix=vendor/libfoo libfoo main --squash
git subtree push --prefix=vendor/libfoo libfoo my-fixIf you are pushing to a fresh branch name, there is nothing to conflict with. Rejections happen when you reuse a branch that someone else, or an earlier split with different options, has already written to.
Splitting without pushing
git subtree split does the first half of a push and stops. It creates synthetic commits in your local repository and, with -b, points a branch at the tip:
git subtree split --prefix=vendor/libfoo -b libfoo-onlyGit prints the hash of the tip commit. The new branch libfoo-only contains only the history of that directory, with paths at the root. You can inspect it like any other branch:
git log --oneline libfoo-only
git ls-tree --name-only libfoo-onlySplitting is useful in three situations:
- Reviewing before you push. You can see exactly what upstream would receive.
- Extracting a directory into its own repository. If
vendor/libfoobegan as code you wrote inside your project, you can publish it independently. Create the empty repository, then push the split branch to it:
git push https://github.com/example/libfoo-new.git libfoo-only:main- Carrying the work somewhere else. You can open a pull request from the split branch, or hand the branch to a colleague with
git bundle.
The split branch is local and disposable. Delete it with git branch -D libfoo-only once you have published it.
Options worth knowing
| Option | Applies to | What it does |
|---|---|---|
| `-b <branch>` | split | Creates or updates a local branch at the split tip. |
| `--annotate=<text>` | split, push | Prepends the text to each rewritten commit message, so synthetic commits are easy to recognize. |
| `--rejoin` | split, push | Merges the split history back into your own history, so later splits start from that point instead of rescanning everything. |
| `--onto=<commit>` | split, push | Tells Git an existing upstream commit to treat as already split, so it need not recompute older history. |
| `--ignore-joins` | split, push | Ignores earlier rejoin and squash records and recomputes the split from scratch. |
| `-d` | most subcommands | Prints debugging output while the history is processed. |
These options are less common than the plain commands above. Reach for them when the plain commands are too slow or produce unexpected output.
Speed and `--rejoin`
Split has to walk the entire history that reaches the prefix. In a repository with tens of thousands of commits, a split can take minutes, and it repeats the work every time. There are two ways to cut that cost:
- Use
--rejoinon a push or split. Git adds a merge commit that records "the history up to here has been split." Later runs start from that marker. - Keep the subtree out of very large, long-lived repositories if you push frequently.
git subtree push --prefix=vendor/libfoo libfoo my-fix --rejoinThe cost of --rejoin is an extra merge commit in your history. Your colleagues will see it, and it can look odd in git log --graph. It is also an ordinary commit that you can't later undo without rewriting shared history. Use it deliberately, and agree on it with your team first.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Push rejected as non-fast-forward | The upstream branch has commits your split lacks, or an earlier split used different options. | Run `git subtree pull`, then push again. Or push to a new branch name. |
| Git says the prefix does not exist or was never added | The `--prefix` path is wrong, has a different spelling, or you ran the command from a state where that directory is not in the repository. | Use the path relative to the repository root, exactly as it appears in `git ls-files`. |
| Push or split takes very long | Git is scanning a large history on every run. | Use `--rejoin`, or push less often with more changes per push. |
| Upstream branch has no commits you expected | Your changes touched files outside the prefix, so they were left out. | Check with `git log -- vendor/libfoo` which commits actually changed the prefix. |
| Upstream history looks duplicated or odd | The subtree was added with `--squash` at one time and without it at another. | Pick one style per subtree and keep it. |
Practical workflow
A working routine for contributing to an upstream library:
- Pull first:
git subtree pull --prefix=vendor/libfoo libfoo main --squash. - Make library changes in their own commits, with messages written for upstream readers.
- Preview with
git subtree split --prefix=vendor/libfoo -b libfoo-onlyandgit log --oneline libfoo-only. - Push to a feature branch:
git subtree push --prefix=vendor/libfoo libfoo my-fix. - Open a pull request upstream from
my-fix. - After upstream merges it, pull again so your copy matches upstream.
Step 6 matters. After a merge or squash-merge upstream, the same change arrives in your subtree a second time as a pull. Git merges it cleanly if the content is identical, but you may see a conflict if upstream edited the change during review. Resolve it as you would any merge conflict.
Exercise
You committed one change that edited vendor/libfoo/parser.txt and README.md in the parent repository. You run git subtree push --prefix=vendor/libfoo libfoo my-fix. What does the my-fix branch upstream contain from that commit, and what path does the parser file have there?
Show answer
It contains only the parser change, with the commit's original author and message. The README change is left out because it is outside the prefix. The file appears as parser.txt at the repository root, because the vendor/libfoo/ prefix is stripped.
Exercise
Your teammate pushed new commits to upstream my-fix, and your git subtree push to that branch is now rejected. Give two ways to get unblocked and say which is safer.
Show answer
Either run git subtree pull --prefix=vendor/libfoo libfoo my-fix and then push again, or push to a new branch name such as my-fix-2. Both are safe. Forcing the push is not, because it would overwrite your teammate's commits. A new branch name is the simplest option when you do not want to merge with their work.
Exercise
What command creates a local branch named libfoo-only that holds only the history of vendor/libfoo, and how would you publish it as main in a brand-new empty repository at a URL you call NEW_URL?
Show answer
Run git subtree split --prefix=vendor/libfoo -b libfoo-only, then git push NEW_URL libfoo-only:main.