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