Submodulesの追加とクローン
プロジェクトに新しいsubmoduleを追加するコマンドと、submodulesを含むリポジトリを正しくクローンする方法を学びます。
「Submodulesの追加とクローン」はCoddyKit上の無料Git Advanced: Monorepo, Submodules & Workflowsレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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.
よくある質問
「Submodulesの追加とクローン」レッスンは無料ですか?
はい。「Submodulesの追加とクローン」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Git Advanced: Monorepo, Submodules & Workflowsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Git Advanced: Monorepo, Submodules & Workflowsコースには全4レッスンが含まれています。
「Submodulesの追加とクローン」で何を学びますか?
プロジェクトに新しいsubmoduleを追加するコマンドと、submodulesを含むリポジトリを正しくクローンする方法を学びます。 ブラウザで直接実行するハンズオンコードでGit Advanced: Monorepo, Submodules & Workflowsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Git Advanced: Monorepo, Submodules & Workflowsを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのGit Advanced: Monorepo, Submodules & Workflowsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「Submodulesの追加とクローン」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このGit Advanced: Monorepo, Submodules & Workflowsレッスンでコードを書いて実行できますか?
はい。すべてのGit Advanced: Monorepo, Submodules & Workflowsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- Git Submodules入門
- Submodulesの追加とクローン
- Submodulesの更新と同期
- サブモジュールの削除と初期化解除