Sub topic 14
Cloning Repositories with Submodules
This chapter assumes you can run git clone, know what a remote and a branch are, and have authenticated to GitHub over HTTPS or SSH. It covers what a submodule is, why a plain clone leaves it empty, and how to fetch it.
What a submodule is
A submodule is a Git repository nested inside another repository. The outer repository (the superproject) does not copy the inner repository's files into its own history. It records two things: the submodule's URL, in a file named .gitmodules, and one exact commit of that submodule.
This pins a dependency to a known version. Everyone who clones the superproject gets the same submodule commit until someone deliberately updates it.
Why a plain clone looks incomplete
A normal git clone downloads the superproject only. Each submodule appears as an empty directory. Git knows the submodule exists, but it has not downloaded its contents.
git clone https://github.com/OWNER/REPO.git
cd REPO
git submodule statusIn the status output, a leading - before a commit hash means the submodule is not initialized. A missing vendor/lib folder or an empty one is the usual symptom.
Cloning with submodules in one step
Add --recurse-submodules to initialize and fetch every submodule during the clone.
git clone --recurse-submodules https://github.com/OWNER/REPO.gitGit clones the superproject, then clones each submodule and checks out the commit the superproject recorded. If a submodule contains its own submodules, Git follows those too.
Fixing a clone you already made
If you cloned without the flag, you do not need to start over. Run this inside the repository:
git submodule update --init --recursive--init registers the submodules listed in .gitmodules. update fetches them and checks out the recorded commits. --recursive covers nested submodules.
Submodules are detached by default
A submodule is checked out at a specific commit, not on a branch. If you run git status inside one, you will see a detached HEAD. Create or switch to a branch before you commit in a submodule, or your commits can become hard to find.
cd vendor/lib
git switch mainAfter you commit and push in the submodule, return to the superproject. It will show the submodule as modified. Commit that change to record the new pinned commit.
Keeping submodules current
Pulling the superproject does not update submodule contents. After git pull, run the update command again:
git pull
git submodule update --init --recursiveAlternatively, run git config submodule.recurse true once. Many commands, including pull and checkout, then update submodules for you.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Submodule folder is empty | Cloned without `--recurse-submodules` | Run `git submodule update --init --recursive` |
| Authentication fails on a submodule only | Submodule URL uses a different protocol or a private repository you cannot access | Check the URL in `.gitmodules` and your access |
| `fatal: not our ref` | The superproject points to a commit that was never pushed in the submodule | Ask the author to push the submodule commit |
| Detached HEAD in the submodule | Normal behavior | Switch to a branch before committing |
Exercise
You cloned a repository and found the vendor/lib directory empty. What single command fixes this without recloning?
Show answer
Run git submodule update --init --recursive inside the repository. It registers the submodules, downloads them, and checks out the commits the superproject recorded.
Exercise
Why does git pull in the superproject not update a submodule's files, and what should you run afterward?
Show answer
The superproject only records which submodule commit to use. Pulling updates that record but does not change the submodule's working files. Run git submodule update --init --recursive, or set submodule.recurse to true.