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

บทนำสู่ Git Subtree

ทำความเข้าใจว่า Git Subtree คืออะไร แตกต่างจาก Submodule อย่างไร และมีประโยชน์หลักต่อการผสานโครงการอย่างไร

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

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Discover Git Subtree

Welcome! In this lesson, we'll explore Git Subtree. It's a way to embed another Git repository inside your own, essentially merging its history and files into a subdirectory.

Think of it as an alternative to Git Submodules for managing external dependencies or shared code within a single project.

Subtree vs. Submodule: Key Difference

The core difference between Subtree and Submodule lies in how they integrate external code:

  • Submodules: Store a reference (a specific commit hash) to an external repository. The external code lives separately, managed as a distinct repository.
  • Subtree: Integrates the external repository's code and its history directly into your main repository. It's like a deep copy with the ability to link back to the original.

How Subtree Works Internally

When you add a subtree, Git performs a specialized merge operation. It takes the history of the external repository and 'rewrites' it to appear as if it always existed within a specific subdirectory of your main project.

This means you can treat the subtree's files just like any other files in your repository, using standard Git commands.

Benefit 1: Simplified Workflow

One major advantage of Git Subtree is its simplicity. There are no special .gitmodules files to track, nor do you need to initialize or update submodules after cloning.

Your entire project, including the subtree's content, is managed with standard Git commands like git clone and git pull.

Benefit 2: Everything in One Repo

With Git Subtree, all your code lives in a single repository. This can simplify several aspects of development:

  • Cloning: A single git clone command fetches everything.
  • Branching: Changes across your main project and the subtree can be made on one branch.
  • CI/CD: Builds and tests typically don't need to fetch separate repositories.

Benefit 3: Smoother Collaboration

Many developers find subtrees easier to work with. Collaborators don't need to learn new Git commands specific to submodules or deal with their unique quirks.

The external code is simply part of the main repository, reducing potential friction when setting up development environments or fetching updates.

Adding a Subtree: The Command

To add a new external project as a subtree, you use the git subtree add command. You'll specify:

  • The prefix (the subdirectory where it will live).
  • The URL of the external repository.
  • The branch you want to pull from (e.g., main or master).

Example: Adding a Subtree

Let's say you want to add a common utility library from GitHub into a vendor/utils directory in your project:

git subtree add --prefix=vendor/utils \
  https://github.com/myorg/common-utils.git main \
  --squash

The `--squash` Option Explained

Notice the --squash option in the previous example. This is very useful when adding a subtree!

It takes all the commits from the external repository's history and condenses them into a single commit when adding them to your main project. This prevents your main repository's history from being cluttered with potentially hundreds of commits from the external project.

Quick Check

Git Subtree offers distinct advantages over submodules in certain scenarios. Let's test your understanding.

Recap: Git Subtree Basics

Great job! In this lesson, we introduced Git Subtree, a powerful alternative to submodules for integrating external projects. You learned that subtrees merge external code directly into your repository, simplifying workflows and keeping all code in one place.

We also covered the git subtree add command and the benefit of the --squash option for cleaner history.

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

บทเรียน “บทนำสู่ Git Subtree” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “บทนำสู่ Git Subtree”

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

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

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

บทเรียน “บทนำสู่ Git 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