使用子树拆分将变更推送回上游
了解如何提取子树内部产生的变更,并使用 git subtree split 和 push 将其推送回原始上游仓库。
使用子树拆分将变更推送回上游 是 CoddyKit 上的免费 Git Advanced: Monorepo, Submodules & Workflows 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Git Advanced: Monorepo, Submodules & Workflows 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Git Advanced: Monorepo, Submodules & Workflows 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Two-Way Subtree Workflows
Subtree is not just for pulling code in. One of its strengths is the ability to contribute changes back to the original project. This makes subtree a genuine two-way integration tool.
The key command is git subtree push, powered internally by git subtree split.
What 'split' Does
git subtree split walks your history and produces a synthetic branch containing only the commits that touched the subtree's prefix, rewritten as if that folder were the repo root.
This is what makes the changes mergeable back upstream.
Recap: Adding a Subtree
Assume you added an upstream library under vendor/widget earlier. Now you have made improvements there and want to share them.
git subtree add --prefix=vendor/widget \
https://example.com/widget.git main --squashCreating the Split Branch
Run split to create a branch with just the subtree's history rebased to the prefix root.
git subtree split --prefix=vendor/widget -b widget-upstreamInspecting the Split
Check out the split branch and confirm its files live at the root, not under vendor/widget. This proves the rewrite worked.
git log --oneline widget-upstream
git ls-tree --name-only widget-upstreamPushing the One-Step Way
You can split and push in a single command. git subtree push does the split internally and pushes the result to the upstream remote and branch.
git subtree push --prefix=vendor/widget \
https://example.com/widget.git contrib-branchOpening a Pull Request
Push to a feature branch on a fork you control, then open a pull request upstream. Your subtree commits appear as ordinary commits on that project, ready for review.
Keeping Splits Fast
On large histories, split can be slow because it re-walks commits. Subtree caches rewrites; passing --rejoin writes a merge commit so future splits start from there.
git subtree split --prefix=vendor/widget --rejoinAvoiding Mixed Commits
Split works cleanly only when commits are scoped. A commit touching both the subtree and unrelated app code becomes awkward upstream. Keep subtree changes in their own focused commits.
When to Push Back
Push upstream when your fixes are general-purpose and the maintainers accept contributions. For private, repo-specific tweaks, keep them local and never push them back.
Pulling Their Acceptance Back
After upstream merges your contribution, pull it back into the subtree so your superproject and the original project converge again, closing the round trip.
git subtree pull --prefix=vendor/widget \
https://example.com/widget.git main --squashQuick Check
Test your understanding of subtree split and push.
Recap
You learned the two-way subtree workflow: split isolates subtree commits into a prefix-rooted branch, push sends them upstream in one step, --rejoin speeds up repeated splits, and keeping commits focused makes upstream contribution clean.
常见问题解答
「使用子树拆分将变更推送回上游」课时是免费的吗?
是的 — 「使用子树拆分将变更推送回上游」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Git Advanced: Monorepo, Submodules & Workflows 课程的其余内容,请升级到 CoddyKit PRO。 Git Advanced: Monorepo, Submodules & Workflows 课程共包含 4 节课。
「使用子树拆分将变更推送回上游」这节课中我会学到什么?
了解如何提取子树内部产生的变更,并使用 git subtree split 和 push 将其推送回原始上游仓库。 你通过在浏览器中直接运行的动手代码来练习 Git Advanced: Monorepo, Submodules & Workflows,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Git Advanced: Monorepo, Submodules & Workflows 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Git Advanced: Monorepo, Submodules & Workflows 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「使用子树拆分将变更推送回上游」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Git Advanced: Monorepo, Submodules & Workflows 课中编写并运行代码吗?
能。每节 Git Advanced: Monorepo, Submodules & Workflows 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。