Sub topic 4
Cloning a project that has submodules
This chapter assumes you know the model from the previous chapters. A submodule is a gitlink entry in the parent repository's tree, and the .gitmodules file maps its path to a URL. You should also be comfortable with git clone, remotes, and the idea that a commit hash identifies exactly one snapshot.
By the end you will be able to:
- Explain why a plain
git cloneleaves submodule directories empty. - Clone a project and populate its submodules in one step or in two.
- Read
git submodule statusoutput. - Recognize and fix the usual clone-time errors.
What a plain clone gives you
Suppose app contains the submodule vendor/libfoo from the earlier chapter. You clone it the ordinary way:
git clone https://github.com/example/app.git
cd app
ls vendor/libfooThe ls prints nothing. The directory exists, but it is empty.
The reason is how Git stores a submodule. The parent repository holds two things:
- A gitlink entry in its tree. It records the path
vendor/libfooand one commit hash. - A
.gitmodulesfile. It records the URL to clone that path from.
Neither one contains any of libfoo's files or history. Git does not fetch a second repository unless you ask it to, because the URL could point anywhere and could require different credentials. So the clone creates an empty placeholder directory where the gitlink sits.
You can confirm this state with git submodule status:
git submodule statusThe output looks like this:
-3f2a9c1d7e5b8a4c6d0e1f2a3b4c5d6e7f8a9b0c vendor/libfooThe leading - means the submodule is not initialized. The hash is the commit the parent expects to find there.
The three states of a submodule
A submodule moves through three stages after you clone. Knowing them makes the commands easier to remember.
- Recorded. The gitlink and
.gitmodulesentry exist in the parent's tree. This is the state right after a plain clone. - Initialized (registered). Git has copied the URL from
.gitmodulesinto your local.git/config. From now on, your local config is the URL Git uses for that submodule. - Checked out. Git has cloned the submodule's repository and checked out the exact commit the gitlink names. A copy of the submodule's
.gitdata is stored under the parent's.git/modules/directory.
The two commands you will use map onto those stages:
| Command | What it does |
|---|---|
| `git submodule init` | Copies URLs from `.gitmodules` into `.git/config`. Does not fetch anything. |
| `git submodule update` | Clones (if needed) and checks out the commit recorded in the parent, for initialized submodules. |
| `git submodule update --init` | Runs both steps. |
| `git submodule update --init --recursive` | Does the same, and also handles submodules nested inside submodules. |
Two ways to get the files
Option 1: clone and populate in one step
# Clone and fetch all submodules in one step
git clone --recurse-submodules https://github.com/example/app.git
# Or, if you already cloned without that flag
git submodule update --init --recursiveUse the first form when you are starting fresh. Use the second when you forgot the flag, or when you pulled a commit that added a new submodule to a repository you already had.
After either command, git submodule status changes:
3f2a9c1d7e5b8a4c6d0e1f2a3b4c5d6e7f8a9b0c vendor/libfoo (v1.4.2)The leading space means the submodule is checked out at exactly the commit the parent records. The text in parentheses comes from git describe, so you see a tag name when one is reachable.
Option 2: initialize selectively
You do not have to fetch every submodule. A large project may have a dozen, and you may need one. Give a path to the command:
git clone https://github.com/example/app.git
cd app
git submodule update --init vendor/libfooOnly vendor/libfoo is initialized and fetched. The others stay in the recorded state. This also lets you skip submodules that hold large assets you do not need locally.
Reading git submodule status
The first character of each line tells you the state. Check it whenever a build fails with "file not found" in a vendored directory.
| Prefix | Meaning | Usual fix |
|---|---|---|
| `-` | Not initialized | `git submodule update --init` |
| (space) | Checked out at the recorded commit | Nothing to do |
| `+` | Checked-out commit differs from the one the parent records | Either `git submodule update` to reset it, or commit the new pointer in the parent if the change was intended |
| `U` | Merge conflict in the gitlink | Resolve the pointer, then `git add` the path |
The + state is the one that surprises people. It does not mean something is broken. It means the submodule's HEAD and the parent's recorded hash disagree. You will see it after you pull parent changes that move a pointer but have not yet run git submodule update.
Detached HEAD is expected
After git submodule update, enter the submodule and run git status:
cd vendor/libfoo
git statusHEAD detached at 3f2a9c1
nothing to commit, working tree cleanGit checked out a commit, not a branch, because the parent's gitlink names a commit. This is correct behavior. It guarantees that everyone who clones the parent at a given commit gets identical submodule contents.
The consequence is that if you start editing here and commit, your new commits belong to no branch. Switching away can leave them reachable only through the reflog. The chapter on changing code inside a submodule covers the safe workflow. For now, the rule is: before you commit in a submodule, create or switch to a branch with git switch -c or git switch.
Nested submodules
A submodule can contain submodules of its own. Without --recursive, Git fetches only the first level. The deeper directories remain empty, and the error you see is often a missing header or import from a dependency of a dependency.
Always include --recursive in scripts and CI jobs unless you know the project has no nesting. With git clone, the --recurse-submodules flag is already recursive. With git submodule update, you must add --recursive yourself.
Speeding up large clones
Submodule clones run one after another by default. Several options reduce the wait.
git clone --recurse-submodules --jobs 4 https://github.com/example/app.git--jobs 4 fetches up to four submodules in parallel. The same flag works on git submodule update.
For CI, where you need the files but not the history, combine shallow clones:
git clone --depth 1 --recurse-submodules --shallow-submodules \
https://github.com/example/app.git--depth 1 shallows the parent. --shallow-submodules does the same for each submodule.
Shallow submodule clones have one catch. The gitlink names a specific commit. If that commit is not at the tip of any branch Git fetched, a depth-1 fetch cannot reach it. Newer Git versions try to fetch the exact commit by hash when the server permits it. Some servers do not. If you see an error that the commit could not be fetched, drop --shallow-submodules for that project.
Configuration that reduces mistakes
You can tell Git to treat submodules as part of routine commands. The following setting makes git checkout, git switch, git pull, and similar commands also update checked-out submodules to match the parent:
git config --global submodule.recurse trueThis setting does not affect git clone. Recent Git versions have a separate clone.recurseSubmodules setting for that. If you rely on it, check that your team's Git versions support it, or keep using the explicit flag.
Even with submodule.recurse on, it only updates submodules that are already initialized. A brand-new submodule added upstream still needs one git submodule update --init.
Common clone-time errors
The directory is empty
Cause: you did a plain clone. Fix: run git submodule update --init --recursive.
"fatal: No url found for submodule path"
Cause: the gitlink exists in the tree, but .gitmodules has no entry for that path. This usually happens when someone committed a nested repository with git add instead of git submodule add, or when someone deleted the .gitmodules entry by hand. Fix: ask the author to add the entry, or add it yourself with git config -f .gitmodules submodule.<name>.url <url> and git config -f .gitmodules submodule.<name>.path <path>. Then run git submodule update --init.
"Permission denied" or authentication failures
Cause: the URL in .gitmodules uses a protocol or host you cannot reach. A common example is an ssh:// or git@ URL when you only have HTTPS credentials. Fix: override the URL locally without changing the committed file:
git config submodule.vendor/libfoo.url https://github.com/example/libfoo.git
git submodule update --init vendor/libfooThis edits .git/config, which is never committed. Note that git submodule init will not overwrite a URL that is already there. If the committed URL later changes, run git submodule sync to copy the new value into your local config.
"not our ref" or "reference is not a tree"
Cause: the parent records a commit that does not exist on the submodule's remote. The usual reason is that someone committed the pointer in the parent but never pushed the commit inside the submodule. The error looks like this:
fatal: remote error: upload-pack: not our ref 3f2a9c1d7e5b8a4c6d0e1f2a3b4c5d6e7f8a9b0c
Fetched in submodule path 'vendor/libfoo', but it did not contain 3f2a9c1d...Nothing on your side can fix it. The person who made the parent commit must push the submodule commit. The git push --recurse-submodules=check command, covered in a later chapter, exists to prevent this.
Relative URLs
A .gitmodules entry may use a relative URL such as ../libfoo.git. Git resolves it against the parent's remote URL. This keeps the parent and its submodules on the same host, which helps with mirrors and forks. If you clone the parent from a mirror, the submodules come from the same mirror. The catch is that a fork of the parent needs forks of the submodules at the matching relative location, or the update fails.
A checklist for a fresh clone
- Clone with
git clone --recurse-submodules(add--jobsfor speed). - Run
git submodule status. Every line should start with a space. - If a line starts with
-, rungit submodule update --init --recursive. - If a line starts with
+, decide whether the difference is intentional. If not, rungit submodule update. - Before editing inside a submodule, switch to a branch there.
Exercise
You run git clone https://github.com/example/app.git, open vendor/libfoo, and find it empty. git submodule status prints a line that starts with -. Which single command fixes this, including any submodules nested inside libfoo, and why was the directory empty?
Show answer
Run git submodule update --init --recursive. A plain clone fetches only the parent repository. It stores the gitlink commit and the .gitmodules URL but does not clone the submodule, so the directory is an empty placeholder. The - prefix means the submodule is not initialized. --init registers the URL and clones it, and --recursive covers nested submodules.
Exercise
After pulling new commits in the parent repository, git submodule status shows a + before vendor/libfoo. Does this mean the clone is corrupted? What command brings the submodule in line with the parent?
Show answer
No. The + means the submodule's checked-out commit differs from the commit the parent now records, which is expected after a pull that moved the pointer. Run git submodule update (with --recursive if there is nesting) to check out the recorded commit. You could set git config --global submodule.recurse true so that git pull does this automatically for already-initialized submodules.
Exercise
A CI job fails with "not our ref" while fetching a submodule during git submodule update --init. Who has to act to fix it, and what went wrong?
Show answer
The author of the parent commit has to act. The parent records a submodule commit hash that was never pushed to the submodule's remote, so no clone can fetch it. The author must push that commit from inside the submodule (or point the parent at a commit that exists on the remote) and then push the parent again.