บทนำสู่ Git Submodule
ทำความเข้าใจว่า Git Submodule คืออะไร กรณีการใช้งาน และช่วงเวลาที่เหมาะสมสำหรับใช้จัดการการพึ่งพา
บทนำสู่ Git Submodule เป็นบทเรียน Git Advanced: Monorepo, Submodules & Workflows ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Git Advanced: Monorepo, Submodules & Workflows และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Git Advanced: Monorepo, Submodules & Workflows มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Welcome to Submodules
What are Git Submodules? They let you embed one Git repository inside another as a sub-directory. It's like having a separate project living inside your main project, but still managed independently.
Why Use Submodules?
Imagine your project needs a specific library or component that's developed and maintained separately. Instead of copying its code, you can use a submodule.
- Vendor Code: Include third-party libraries.
- Shared Components: Reusable code across multiple projects.
- Plugin Systems: Modularize parts of a larger application.
A Repo within a Repo
A submodule isn't just a folder of files. It's a pointer to a specific commit in another Git repository. Your main project tracks which commit of the submodule it should use.
This means your main project doesn't directly manage the submodule's content, but rather its reference.
How Git Tracks Submodules
Git stores submodules as a special entry in your main repository's index. It records the submodule's path, the URL of its remote repository, and the specific commit hash it currently points to.
When you clone a project with submodules, you'll initially see empty directories until you initialize and update them.
Submodule's Independent Life
Each submodule is its own standalone Git repository. You can clone it, switch branches, commit changes, and push them, all independently of the parent repository.
This isolation is key to managing external dependencies without merging their history into your main project.
Common Use Cases
Submodules are excellent for:
- Stable Dependencies: When you need a specific version of an external library and don't want it to change unexpectedly.
- Large Components: Breaking down a monolithic project into smaller, manageable, independently versioned parts.
- Open-Source Contributions: Integrating external tools or frameworks without maintaining their source directly.
When Submodules Shine
They are particularly useful when:
- You need to track a specific version of a dependency, not just the latest.
- The dependency is managed by a different team or project.
- You want to keep the dependency's history separate from your main project's history.
The .gitmodules File
When you add a submodule, Git creates a special configuration file at the root of your main repository: .gitmodules.
This file stores the mapping between the submodule's local path and its remote URL. It's version-controlled, so everyone cloning your project knows about its submodules.
[submodule "library"]
path = lib/my-library
url = https://github.com/user/my-library.gitSubtree vs. Submodule (Brief)
While submodules are great, they have alternatives. One notable option is Git Subtree.
Submodules keep the dependency's history separate, while Git Subtree integrates the dependency's history directly into your main repository. We'll explore Subtree in a later lesson!
Quick Check on Submodules
Let's check your understanding of Git submodules.
Recap: Intro to Submodules
We've introduced Git submodules, understanding them as a way to embed independent Git repositories within your main project.
- They track specific commits of external repos.
- They solve problems of shared code and stable dependencies.
- They are configured via the
.gitmodulesfile.
Next, we'll learn how to add and clone submodules.
คำถามที่พบบ่อย
บทเรียน “บทนำสู่ Git Submodule” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “บทนำสู่ Git Submodule” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Git Advanced: Monorepo, Submodules & Workflows ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Git Advanced: Monorepo, Submodules & Workflows มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “บทนำสู่ Git Submodule”
ทำความเข้าใจว่า Git Submodule คืออะไร กรณีการใช้งาน และช่วงเวลาที่เหมาะสมสำหรับใช้จัดการการพึ่งพา คุณปฏิบัติ Git Advanced: Monorepo, Submodules & Workflows ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Git Advanced: Monorepo, Submodules & Workflows หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Git Advanced: Monorepo, Submodules & Workflows บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “บทนำสู่ Git Submodule” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Git Advanced: Monorepo, Submodules & Workflows นี้ได้ไหม
ได้ บทเรียน Git Advanced: Monorepo, Submodules & Workflows ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- บทนำสู่ Git Submodule
- การเพิ่มและโคลน Submodule
- การอัปเดตและซิงโครไนซ์ Submodule
- การลบและยกเลิกการเริ่มต้นซับโมดูล