การเพิ่มและผสานด้วย Subtree
เรียนรู้คำสั่งสำหรับเพิ่มคลังภายนอกเป็น Subtree และวิธีดึงการอัปเดตจากต้นทางของคลัง
การเพิ่มและผสานด้วย Subtree เป็นบทเรียน Git Advanced: Monorepo, Submodules & Workflows ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Git Advanced: Monorepo, Submodules & Workflows และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Git Advanced: Monorepo, Submodules & Workflows มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Subtree: Integrating External Code
Welcome! In this lesson, we'll dive into Git Subtree, a powerful way to integrate another Git repository directly into your project.
Unlike submodules, subtree merges the external repository's history into your own, making it feel like a native part of your project.
Why Use `git subtree add`?
Subtree offers a simpler workflow than submodules for many cases. Here are a few benefits:
- No special commands: Once added, you work with subtree content like regular files.
- Single repository: No separate
.gitdirectories or complex submodule initializations. - Easier for consumers: Others cloning your project don't need to learn special submodule commands.
Preparing Your Repository
Before adding a subtree, ensure your main repository is in a clean state (no uncommitted changes). You'll need:
- The URL of the external repository.
- The branch or commit you want to integrate (e.g.,
mainormaster). - A chosen path within your main repository for the subtree.
The `git subtree add` Command
To add an external repository as a subtree, you use the git subtree add command. It looks like this:
git subtree add --prefix=<path> <repository> <ref>
--prefix=<path>: The directory in your project where the subtree will reside.<repository>: The URL of the external Git repository.<ref>: The branch (e.g.,main) or commit hash from the external repo.
Adding an Example Subtree
Let's say you want to add a shared utility-library from a separate Git repository into a src/shared folder in your current project. You would use:
git subtree add --prefix=src/shared/utility-library https://github.com/myorg/utility-library.git main
This command fetches the utility-library's main branch and integrates its history into your src/shared/utility-library directory.
Using the `--squash` Option
When adding a subtree, you'll often see the --squash option:
git subtree add --prefix=path repo_url main --squash
The --squash flag combines all commits from the external repository's history into a single commit in your main project. This keeps your main project's history cleaner, but you lose the individual commit details from the subtree's original history.
Why Update a Subtree?
After you've added a subtree, the original external repository might receive new updates. To keep your integrated code current with the upstream source, you'll need to pull those changes.
This ensures your project always has the latest versions of the shared components or libraries you've included as a subtree.
The `git subtree pull` Command
To pull updates from an upstream subtree, you use a similar command structure:
git subtree pull --prefix=<path> <repository> <ref>
Notice it uses the same --prefix, <repository>, and <ref> as when you added it. Git uses these to identify the correct upstream source for your subtree.
Updating the Example Subtree
Let's continue with our utility-library example. To pull the latest changes from its main branch into your project, you'd run:
git subtree pull --prefix=src/shared/utility-library https://github.com/myorg/utility-library.git main
This command fetches and merges any new commits from the utility-library's main branch into your local src/shared/utility-library directory.
Subtree Command Check
Which command is used to initially incorporate an external repository as a subtree into your project, creating a new directory for it?
Recap: Adding & Merging Subtrees
You've learned how to integrate external repositories using Git Subtree!
- Use
git subtree addwith--prefix, repository URL, and branch to bring external code into your project. - The
--squashoption helps keep your main project's history clean. - Use
git subtree pullwith the same parameters to fetch and merge upstream updates into your subtree.
Subtree provides a powerful, integrated alternative to submodules for managing shared code!
คำถามที่พบบ่อย
บทเรียน “การเพิ่มและผสานด้วย Subtree” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การเพิ่มและผสานด้วย Subtree” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Git Advanced: Monorepo, Submodules & Workflows ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Git Advanced: Monorepo, Submodules & Workflows มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การเพิ่มและผสานด้วย Subtree”
เรียนรู้คำสั่งสำหรับเพิ่มคลังภายนอกเป็น Subtree และวิธีดึงการอัปเดตจากต้นทางของคลัง คุณปฏิบัติ Git Advanced: Monorepo, Submodules & Workflows ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Git Advanced: Monorepo, Submodules & Workflows หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Git Advanced: Monorepo, Submodules & Workflows บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “การเพิ่มและผสานด้วย Subtree” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Git Advanced: Monorepo, Submodules & Workflows นี้ได้ไหม
ได้ บทเรียน Git Advanced: Monorepo, Submodules & Workflows ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- บทนำสู่ Git Subtree
- การเพิ่มและผสานด้วย Subtree
- การเปรียบเทียบ Subtree กับ Submodule
- การส่งการเปลี่ยนแปลงกลับไปยังต้นทางด้วยการแยก Subtree