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

การเพิ่มและโคลน Submodule

เรียนรู้คำสั่งสำหรับเพิ่ม Submodule ใหม่ลงในโครงการ และวิธีโคลนคลังที่มี Submodule อย่างถูกต้อง

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

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

Add & Clone Git Submodules

Git submodules let you embed one Git repository inside another. This is useful for managing external dependencies like libraries or shared components.

In this lesson, we'll learn how to add a new submodule to your project and how to properly clone a repository that contains submodules.

The `git submodule add` Command

To add a submodule, you use the git submodule add command. This command takes the URL of the external repository and optionally a local path where you want to place it.

  • Repository URL: The URL (HTTPS or SSH) of the submodule's Git repository.
  • Path: The directory within your main project where the submodule will reside. If omitted, Git uses the repository name.

Add Submodule in Action

Let's say you have a my-project repository and want to include a shared library from https://github.com/example/shared-lib.git.

First, navigate to your main project's directory.

cd my-project
git submodule add https://github.com/example/shared-lib.git lib/shared-lib

Files Created by `submodule add`

After running git submodule add, Git does a few things:

  • It clones the submodule repository into the specified path (e.g., lib/shared-lib).
  • It adds an entry to a special file called .gitmodules in your main repository's root.
  • It stages the new submodule directory and the .gitmodules file for commit.

Inspecting `.gitmodules`

The .gitmodules file is a configuration file that stores the mapping between the submodule's local path and its remote URL. It's crucial for others to clone your project correctly.

Here's what it might look like for our example:

[submodule "lib/shared-lib"]
	path = lib/shared-lib
	url = https://github.com/example/shared-lib.git

Commit Your Submodule Change

Adding a submodule is a change to your main repository, so you need to commit it. The commit will record the specific commit hash of the submodule's repository that your main project is currently using.

git status
git commit -m "Add shared-lib as a submodule"

Cloning a Parent Repository

When you clone a repository that contains submodules, Git clones the parent repository but doesn't automatically download the submodule content.

The submodule directories will appear empty or contain only a .git file pointing to the submodule's repository.

The Easy Way to Clone All

The most straightforward way to clone a repository and all its submodules is to use the --recurse-submodules flag with git clone.

This command clones the main repository and then automatically initializes and updates all submodules to their correct versions.

git clone --recurse-submodules https://github.com/your/my-project.git

Manually Initializing Submodules

If you forget --recurse-submodules during cloning, don't worry! You can initialize and update them manually after cloning the parent repository.

First, navigate into the cloned parent repository.

cd my-project
git submodule init

Fetching Submodule Content

After initializing, you need to tell Git to fetch the actual content for each submodule. This is done with the git submodule update command.

It fetches the data from the submodule's remote and checks out the specific commit recorded by the parent repository.

cd my-project
git submodule update

Check Your Submodule Knowledge

You've just cloned a repository named parent-repo which is known to contain a submodule. After cloning with just git clone parent-repo.git, what are the necessary commands to get the submodule's content ready for use?

Recap: Add & Clone Submodules

In this lesson, you learned how to:

  • Add a submodule using git submodule add <url> <path>, which creates a .gitmodules file and stages the submodule.
  • Commit submodule changes to record the specific version of the submodule in the parent repository.
  • Clone a repository with submodules using git clone --recurse-submodules for an all-in-one operation.
  • Manually initialize and update submodules with git submodule init and git submodule update if you cloned without the recurse flag.

Next, we'll explore how to manage and update submodules after they've been added and cloned.

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

บทเรียน “การเพิ่มและโคลน Submodule” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “การเพิ่มและโคลน Submodule”

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

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

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

บทเรียน “การเพิ่มและโคลน Submodule” ใช้เวลานานแค่ไหน

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

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

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

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

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