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

Adding and Cloning Submodules

Learn the commands to add a new submodule to a project and how to properly clone a repository with submodules.

Adding and Cloning Submodules is a free Git Advanced: Monorepo, Submodules & Workflows lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Git Advanced: Monorepo, Submodules & Workflows learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

Is the “Adding and Cloning Submodules” lesson free?

Yes — the full text of “Adding and Cloning Submodules” is free to read here on the web, and the Git Advanced: Monorepo, Submodules & Workflows course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Git Advanced: Monorepo, Submodules & Workflows course, upgrade to CoddyKit PRO.

What will I learn in “Adding and Cloning Submodules”?

Learn the commands to add a new submodule to a project and how to properly clone a repository with submodules. You practise Git Advanced: Monorepo, Submodules & Workflows with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Git Advanced: Monorepo, Submodules & Workflows?

No prior experience is required. Git Advanced: Monorepo, Submodules & Workflows on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Adding and Cloning Submodules” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Git Advanced: Monorepo, Submodules & Workflows lesson?

Yes. Every Git Advanced: Monorepo, Submodules & Workflows lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Introduction to Git Submodules
  2. Adding and Cloning Submodules
  3. Updating and Synchronizing Submodules
  4. Removing and Deinitializing Submodules
← Back to Git Advanced: Monorepo, Submodules & Workflows