git submodule update --init --recursive: Usage & CI Errors
By Kaveh Alemi·Latchkey
git submodule update --init --recursive fetches and checks out every submodule, including nested ones.
A bare clone leaves submodules empty. This one command initializes the config, clones each submodule at the pinned commit, and recurses into submodules-of-submodules - the standard CI bootstrap.
What it does
git submodule update --init reads .gitmodules, registers each submodule, clones any that are missing, and checks out the exact commit the superproject pins. --recursive applies the same process to nested submodules.
fatal: could not read Username for ‘https://...’ / "could not get a repository handle for submodule" - a private submodule needs credentials the runner lacks; use a token, insteadOf URL rewrite, or SSH keys with access. Submodules check out at a detached HEAD by design; commits there need a branch to survive.
Using this in CI
CI checkouts are shallow and detached by default, which changes the answer this command gives you. Commands that read history, branch names, or tags need the checkout configured for it.
.github/workflows/ci.yml
- uses:actions/checkout@v4with:fetch-depth:0 # history, tags, and git describe all need this- run:|git rev-parse --is-shallow-repository # expect falsegit rev-parse --abbrev-ref HEAD # prints HEAD when detached
Frequently asked questions
git submodule update --init --recursive: Usage & CI Errors?
A bare clone leaves submodules empty. This one command initializes the config, clones each submodule at the pinned commit, and recurses into submodules-of-submodules - the standard CI bootstrap.
What it does?
git submodule update --init reads .gitmodules, registers each submodule, clones any that are missing, and checks out the exact commit the superproject pins. --recursive applies the same process to nested submodules.
Common errors in CI?
fatal: could not read Username for ‘https://...’ / "could not get a repository handle for submodule" - a private submodule needs credentials the runner lacks; use a token, insteadOf URL rewrite, or SSH keys with access. Submodules check out at a detached HEAD by design; commits there need a branch to survive.