Sub topic 4
Cloning a Project That Has Submodules
This chapter assumes you know everyday Git (clone, commit, branch, push) and what a commit hash identifies. It also assumes you have read how a submodule is recorded: the parent repository stores a gitlink, which is a commit hash for a path, and .gitmodules maps that path to a URL.
Why a plain clone looks broken
A normal git clone copies the parent repository only. The parent's tree contains a gitlink for each submodule and a .gitmodules file. It does not contain the submodule's files. Git creates an empty directory at each submodule path and stops there.
git clone https://github.com/example/app.git
ls app/vendor/libfooThe ls prints nothing. git status reports a clean tree, so nothing warns you. The first sign is usually a build error such as a missing header or a missing import.
Option 1: clone with submodules in one step
Pass --recurse-submodules when you clone. Git clones the parent, then clones each submodule and checks out the commit the parent records.
# Clone and fetch all submodules in one step
git clone --recurse-submodules https://github.com/example/app.gitThis also handles nested submodules, which are submodules inside submodules. Use this form by default.
Option 2: initialize after a plain clone
If you already cloned without the flag, run this inside the parent repository:
# Or, if you already cloned without that flag
git submodule update --init --recursiveThe command combines two steps that you can also run separately:
git submodule initcopies each submodule's URL from.gitmodulesinto your local.git/config. This is called registering the submodule.git submodule updateclones each registered submodule if it is missing, then checks out the commit that the parent records.
--init runs the first step for you. --recursive repeats both steps for nested submodules. Without --recursive, only the first level is populated.
What state you end up in
After cloning, each submodule is in a detached HEAD state. HEAD points directly at the pinned commit, not at a branch. This is intended: the parent asks for an exact commit, not "whatever is newest on main."
Check it with:
git submodule statusThe output has one line per submodule. The line starts with a character that tells you its state:
| Prefix | Meaning |
|---|---|
| (space) | Checked out at the commit the parent records |
| `-` | Not initialized, so the directory is empty |
| `+` | Checked out at a different commit than the parent records |
| `U` | Merge conflict in the submodule |
A - prefix is the symptom of a plain clone. A + prefix is common after you switch branches in the parent, as the next section explains.
Common errors
Empty submodule directory. You cloned without --recurse-submodules. Run git submodule update --init --recursive.
A + prefix after git pull or git switch. Updating the parent moves the recorded commit, but Git does not touch the submodule's working tree by default. Run git submodule update --init --recursive after changing branches or pulling. Git has a setting that automates this, which a later chapter covers.
"Fetched in submodule path, but it did not contain" a commit. The parent records a commit that the submodule's remote does not have. Someone pushed the parent without pushing the submodule first. You cannot fix this on your side. Ask the author to push the submodule commit.
Authentication failure on a submodule. The URL in .gitmodules may use a protocol or host you cannot access. Check the URL with git config -f .gitmodules --get-regexp url. You can override it locally in .git/config without changing the shared file.
Submodule cloned but on the wrong commit. Do not run git pull inside the submodule to "fix" it. That moves it away from the pinned commit and the parent will show it as modified. Run git submodule update from the parent to return to the recorded commit.
Exercise
A teammate clones your repository with a plain git clone. The build fails because vendor/libfoo/include/foo.h does not exist, yet git status is clean. What single command fixes it, and why was the directory empty?
Show answer
Run git submodule update --init --recursive in the parent repository. A plain clone copies only the parent's tree, which holds a gitlink and .gitmodules for vendor/libfoo but not the submodule's files. Git leaves an empty directory until the submodule is registered, cloned, and checked out at the pinned commit.
Exercise
After running git switch release in the parent, git submodule status shows a + before vendor/libfoo. What does that mean, and how do you resolve it?
Show answer
The submodule is checked out at a different commit than the one the release branch records. Switching branches changed the recorded commit but not the submodule's working tree. Run git submodule update --init --recursive to check out the recorded commit.