0Pricing
Git Advanced: Monorepo, Submodules & Workflows · บทเรียน

การส่งการเปลี่ยนแปลงกลับไปยังต้นทางด้วยการแยก Subtree

เรียนรู้วิธีแยกการเปลี่ยนแปลงที่ทำภายใน subtree แล้วส่งกลับไปยังรีโพซิทอรีต้นทางด้วย git subtree split และ push

การส่งการเปลี่ยนแปลงกลับไปยังต้นทางด้วยการแยก Subtree เป็นบทเรียน Git Advanced: Monorepo, Submodules & Workflows ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 --squash

Creating 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-upstream

Inspecting 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-upstream

Pushing 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-branch

Opening 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 --rejoin

Avoiding 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 --squash

Quick 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.

คำถามที่พบบ่อย

บทเรียน “การส่งการเปลี่ยนแปลงกลับไปยังต้นทางด้วยการแยก Subtree” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การส่งการเปลี่ยนแปลงกลับไปยังต้นทางด้วยการแยก Subtree” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Git Advanced: Monorepo, Submodules & Workflows ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Git Advanced: Monorepo, Submodules & Workflows มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การส่งการเปลี่ยนแปลงกลับไปยังต้นทางด้วยการแยก Subtree”

เรียนรู้วิธีแยกการเปลี่ยนแปลงที่ทำภายใน subtree แล้วส่งกลับไปยังรีโพซิทอรีต้นทางด้วย git subtree split และ push คุณปฏิบัติ Git Advanced: Monorepo, Submodules & Workflows ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Git Advanced: Monorepo, Submodules & Workflows หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Git Advanced: Monorepo, Submodules & Workflows บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การส่งการเปลี่ยนแปลงกลับไปยังต้นทางด้วยการแยก Subtree” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Git Advanced: Monorepo, Submodules & Workflows นี้ได้ไหม

ได้ บทเรียน Git Advanced: Monorepo, Submodules & Workflows ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. บทนำสู่ Git Subtree
  2. การเพิ่มและผสานด้วย Subtree
  3. การเปรียบเทียบ Subtree กับ Submodule
  4. การส่งการเปลี่ยนแปลงกลับไปยังต้นทางด้วยการแยก Subtree
← กลับไปที่ Git Advanced: Monorepo, Submodules & Workflows