Git update submodules recursively
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
To update all Git submodules recursively (including nested submodules), run git submodule update --init --recursive. The --init flag initializes any submodules that have not been cloned yet, and --recursive processes submodules within submodules. This is the most common command for syncing submodules after a git pull or fresh clone. For pulling the latest changes from each submodule's remote, use git submodule update --remote --recursive.
The Essential Commands
Clone with Submodules
Update After Pull
Pull Latest from Submodule Remotes
Understanding the Flags
How Submodules Work
When you run git submodule update, Git checks out commit abc1234 in libs/utils. When you run git submodule update --remote, Git fetches the latest from the remote and checks out the newest commit on the tracked branch.
Updating a Specific Submodule
Recording Submodule Updates
The parent repo does not store the submodule code — it stores a pointer (commit hash). You must commit this pointer change for others to see the update.
Automating with git pull
Foreach: Run Commands in All Submodules
Troubleshooting
Submodule Directory is Empty
Detached HEAD in Submodule
URL Changed
Common Pitfalls
- Forgetting
--initon first clone: Without--init, submodules that have not been cloned are skipped. Always use--initunless you are sure all submodules are already initialized. - Confusing
updatewithupdate --remote:git submodule updatechecks out the commit recorded in the parent repo.git submodule update --remotefetches and checks out the latest from the remote. The former syncs to what the parent expects; the latter updates to the newest code. - Not committing submodule reference changes: After
git submodule update --remote, the parent repo has unstaged changes (new commit hashes). If you do not commit these, other developers and CI will still use the old commits. - Detached HEAD confusion: Submodules are always in detached HEAD state after
update. This is normal. If you need to make changes in a submodule, checkout a branch first. - Nested submodules without
--recursive: If a submodule itself contains submodules,git submodule update --initonly processes the first level. Always include--recursiveto handle all nesting levels.
Summary
- Use
git submodule update --init --recursiveto sync submodules after clone or pull - Use
git submodule update --remote --recursiveto pull latest from submodule remotes - Use
git clone --recurse-submodulesto clone with all submodules in one step - Set
git config submodule.recurse trueto auto-update submodules on pull/checkout - Always commit submodule reference changes in the parent repo after updates
- Use
--recursiveflag to handle nested submodules (submodules within submodules)
Related reading
- Git vs Mercurial vs SVN
- Git What's the best practice to git clone into an existing folder?
- git, whitespace errors, squelching and autocrlf, the definitive answers
- Git with large files
- Git workflow and rebase vs merge questions
- Git workflow and rebase vs merge questions
- git Your branch is ahead by X commits
- Github - unexpected disconnect while reading sideband packet
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.