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

Aggiunta e clonazione dei submodule

Impari i comandi per aggiungere un nuovo submodule a un progetto e per clonare correttamente un repository che contiene submodule.

Aggiunta e clonazione dei submodule è una lezione Git Advanced: Monorepo, Submodules & Workflows gratuita su CoddyKit. Questa è la lezione 2 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Git Advanced: Monorepo, Submodules & Workflows, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Git Advanced: Monorepo, Submodules & Workflows include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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.

Domande Frequenti

La lezione «Aggiunta e clonazione dei submodule» è gratuita?

Sì — il testo completo di «Aggiunta e clonazione dei submodule» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Git Advanced: Monorepo, Submodules & Workflows, passa a CoddyKit PRO. Il corso Git Advanced: Monorepo, Submodules & Workflows include 4 lezioni in totale.

Cosa imparerò in «Aggiunta e clonazione dei submodule»?

Impari i comandi per aggiungere un nuovo submodule a un progetto e per clonare correttamente un repository che contiene submodule. Eserciti Git Advanced: Monorepo, Submodules & Workflows con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Git Advanced: Monorepo, Submodules & Workflows?

Non è richiesta alcuna esperienza precedente. Git Advanced: Monorepo, Submodules & Workflows su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 2 di 4.

Quanto tempo richiede la lezione «Aggiunta e clonazione dei submodule»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Git Advanced: Monorepo, Submodules & Workflows?

Sì. Ogni lezione Git Advanced: Monorepo, Submodules & Workflows include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Introduzione ai Git Submodule
  2. Aggiunta e clonazione dei submodule
  3. Aggiornamento e sincronizzazione dei submodule
  4. Rimuovere e deinizializzare i submodule
← Torna a Git Advanced: Monorepo, Submodules & Workflows