🚀 Don’t miss out!
Subscribe to Learning Ocean to get coupons, early access to blogs/courses, and exclusive videos.
Watch this full topic on My YouTube Channel.
Video link for this topic: <VIDEO_URL>

Cloning a Git Repository with Submodules — Step-by-Step

In the previous blog, we saw how to add submodules to a Git project.
But what happens when someone else clones that project?

If they just run git clone, they won’t get the submodule code—only empty folders and pointers.
In this guide, you’ll learn how to properly initialize and update submodules after cloning.


The Problem: Empty Submodule Folders

Let’s clone a project that already contains submodules:

git clone git@github.com:user/git-tutorials.git
cd git-tutorials

Check the folders:

ls hooks
# empty
ls logger
# empty

Inside .gitmodules you can see the configuration:

cat .gitmodules
# Lists path and URL for each submodule

But inside .git/config, there is no submodule section yet. This is why the submodule directories are empty.


Step 1: Initialize Submodules

Initialize submodules so Git records their configuration:

git submodule init

This adds the submodule details to .git/config:

cat .git/config
# now includes [submodule "logger"] and [submodule "hooks"]

However, the submodule folders are still empty.


Step 2: Update Submodules

Now fetch the actual code:

git submodule update

This command:

  • Clones each submodule’s repository.
  • Checks out the specific commit recorded in the parent repo.

Check again:

ls hooks
# ✅ All hook files present

ls logger
# ✅ Logger files present

One-Step Shortcut: git submodule update --init

Instead of two separate commands, you can do it in one step:

git submodule update --init

This initializes and updates submodules together.


Even Faster: Clone with --recursive

You can skip the manual steps entirely:

git clone --recursive git@github.com:user/git-tutorials.git

This:

  • Clones the main repo.
  • Initializes and updates all submodules in one go.
  • Even works if your submodules themselves contain submodules (thanks to the --recursive flag).

Visual summary:



Key Points to Remember

StepCommandPurpose
1git clone <repo>Clones only the main project
2git submodule initRegisters submodules inside .git/config
3git submodule updateDownloads and checks out each submodule’s code
One-stepgit submodule update --initCombines init and update
Easiestgit clone --recursive <repo>Does everything in one command

Why Don’t Submodules Auto-Update?

Submodules always point to a fixed commit in the parent repository. If the upstream submodule (e.g., a logger library) adds new commits, your cloned project will not automatically get them—this is by design to prevent breaking changes.

In the next blog, we’ll see:

👉 How to pull the latest changes inside a submodule when you really want the newest code.


Keep Learning 🚀

👉 Subscribe to Learning Ocean – Subscribers get coupon codes for my courses, early access to blogs and courses, and even exclusive YouTube videos.

👉 My YouTube Channel – More videos, more fun, and lots of learning!

👉 📺 Watch this topic in video form

Stay curious, keep coding, and let’s make learning fun together! 🎉