إضافة الوحدات الفرعية واستنساخها
تعلّم أوامر إضافة وحدة فرعية جديدة إلى مشروع وكيفية استنساخ مستودع يحتوي على وحدات فرعية بطريقة صحيحة.
إضافة الوحدات الفرعية واستنساخها درس مجاني في Git Advanced: Monorepo, Submodules & Workflows على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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-libFiles 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
.gitmodulesin your main repository's root. - It stages the new submodule directory and the
.gitmodulesfile 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.gitCommit 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.gitManually 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 initFetching 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 updateCheck 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.gitmodulesfile 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-submodulesfor an all-in-one operation. - Manually initialize and update submodules with
git submodule initandgit submodule updateif you cloned without the recurse flag.
Next, we'll explore how to manage and update submodules after they've been added and cloned.
الأسئلة الشائعة
هل درس «إضافة الوحدات الفرعية واستنساخها» مجاني؟
نعم — نص درس «إضافة الوحدات الفرعية واستنساخها» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Git Advanced: Monorepo, Submodules & Workflows، انتقل إلى CoddyKit PRO. تتضمن دورة Git Advanced: Monorepo, Submodules & Workflows 4 دروس في المجموع.
ماذا ستتعلم في «إضافة الوحدات الفرعية واستنساخها»؟
تعلّم أوامر إضافة وحدة فرعية جديدة إلى مشروع وكيفية استنساخ مستودع يحتوي على وحدات فرعية بطريقة صحيحة. تتمرن على Git Advanced: Monorepo, Submodules & Workflows مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Git Advanced: Monorepo, Submodules & Workflows؟
لا تُشترط خبرة سابقة. Git Advanced: Monorepo, Submodules & Workflows على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «إضافة الوحدات الفرعية واستنساخها»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Git Advanced: Monorepo, Submodules & Workflows هذا؟
نعم. كل درس في Git Advanced: Monorepo, Submodules & Workflows يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- مقدمة إلى الوحدات الفرعية في Git
- إضافة الوحدات الفرعية واستنساخها
- تحديث الوحدات الفرعية ومزامنتها
- إزالة الوحدات الفرعية وإلغاء تهيئتها