git submodule update --init gives error fatal Needed a single revision Unable to find current revision in submodule path
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
The error fatal: Needed a single revision. Unable to find current revision in submodule path 'path/to/submodule' occurs when Git cannot find the commit that the parent repository expects in the submodule. This typically happens when the submodule directory exists but is empty or corrupted, the submodule's remote URL has changed, or the referenced commit was force-pushed away. The fix depends on the root cause — usually a combination of removing the submodule directory and re-initializing it.
Understanding the Error
When you run git submodule update --init, Git does three things:
- Reads
.gitmodulesto get the submodule URL - Clones the submodule repository into the specified path
- Checks out the specific commit recorded in the parent repository's tree
The error occurs at step 3 — the submodule directory exists but Git cannot find a valid revision in it. This means the directory is not a valid Git repository, or the expected commit does not exist in it.
Fix 1: Remove and Re-Initialize the Submodule
The most common fix — delete the submodule directory and let Git re-clone it:
The .git/modules/ directory caches submodule metadata. If this cache is stale, Git uses the cached (broken) data instead of re-cloning. Removing both the directory and the cache forces a fresh clone.
Fix 2: Deinit and Reinit
git submodule deinit unregisters the submodule and removes its working tree. The -f flag forces removal even if there are local changes.
Fix 3: Fix the Submodule URL
If the submodule's remote URL has changed (repo moved, renamed, or transferred):
Fix 4: The Referenced Commit No Longer Exists
If the submodule's commit was force-pushed away or the branch was deleted:
Fix 5: Empty Submodule Directory
If the submodule directory exists but is empty (common after manual git clone without --recursive):
An empty directory blocks Git from cloning into it because Git expects either a valid repository or a non-existent path.
Diagnosing the Issue
Preventing the Issue
Common Pitfalls
- Not removing
.git/modules/cache: Deleting the submodule directory without removing.git/modules/path/to/submoduleleaves stale metadata. Git uses the cached (broken) clone instead of re-cloning. Always remove both the directory and the cache. - Cloning without
--recursive:git clonedoes not fetch submodules by default. Runninggit submodule update --initafter a plain clone works, but if interrupted, it leaves empty directories that cause this error on retry. - Changing submodule URLs without running
git submodule sync: Editing.gitmodulesupdates the URL in the file but not in.git/config. Withoutgit submodule sync, Git still uses the old URL from.git/config. - Force-pushing the submodule repository: If someone force-pushes the submodule repo and deletes the commit that the parent references,
git submodule updatefails because the expected commit no longer exists. Pin submodules to tagged releases to avoid this. - Using relative paths in
.gitmoduleswith different remotes: Relative submodule URLs (e.g.,../other-repo.git) resolve against the parent's remote URL. If you clone from a fork but the submodule path is relative, it resolves to the fork's organization which may not have the submodule repo.
Summary
- The error means Git cannot find the expected commit in the submodule directory
- First try: remove the submodule directory and
.git/modules/cache, then rungit submodule update --init - If the URL changed, update
.gitmodulesand rungit submodule sync - If the commit was force-pushed away, update the submodule to a new valid commit
- Always clone with
--recursiveand rungit submodule update --init --recursiveafter pulling - Check submodule status with
git submodule statusto diagnose which submodules are broken
Related reading
- git submodule update --remote vs git pull
- git submodule update failed with 'fatal detected dubious ownership in repository at...
- git svn - Can I use git and svn at the same time? no need interaction between git and svn
- git svn clone malformed index info error
- git tries to delete a directory on checkout
- Github - unexpected disconnect while reading sideband packet
- git switch branch without discarding local changes
- Git symbolic links in Windows
.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 courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseInterview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.