0Pricing
Git Advanced: Monorepo, Submodules & Workflows · Lektion

Submodule hinzufügen und klonen

Lernen Sie die Befehle zum Hinzufügen eines neuen Submoduls zu einem Projekt und zum korrekten Klonen eines Repositorys mit Submodulen.

Submodule hinzufügen und klonen ist eine kostenlose Git Advanced: Monorepo, Submodules & Workflows-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Git Advanced: Monorepo, Submodules & Workflows-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Git Advanced: Monorepo, Submodules & Workflows-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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.

Häufig gestellte Fragen

Ist die Lektion „Submodule hinzufügen und klonen“ kostenlos?

Ja — der vollständige Text von „Submodule hinzufügen und klonen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Git Advanced: Monorepo, Submodules & Workflows-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Git Advanced: Monorepo, Submodules & Workflows-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Submodule hinzufügen und klonen“?

Lernen Sie die Befehle zum Hinzufügen eines neuen Submoduls zu einem Projekt und zum korrekten Klonen eines Repositorys mit Submodulen. Du übst Git Advanced: Monorepo, Submodules & Workflows mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Git Advanced: Monorepo, Submodules & Workflows zu starten?

Keine Vorkenntnisse erforderlich. Git Advanced: Monorepo, Submodules & Workflows auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.

Wie lange dauert die Lektion „Submodule hinzufügen und klonen“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Git Advanced: Monorepo, Submodules & Workflows-Lektion Code schreiben und ausführen?

Ja. Jede Git Advanced: Monorepo, Submodules & Workflows-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Einführung in Git-Submodule
  2. Submodule hinzufügen und klonen
  3. Submodule aktualisieren und synchronisieren
  4. Submodule entfernen und deinitialisieren
← Zurück zu Git Advanced: Monorepo, Submodules & Workflows