Git
Submodule
Error
Revision
Troubleshooting

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.

Browse interview questions

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:

  1. Reads .gitmodules to get the submodule URL
  2. Clones the submodule repository into the specified path
  3. 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.

bash
git submodule update --init
# fatal: Needed a single revision
# Unable to find current revision in submodule path 'libs/mylib'

Fix 1: Remove and Re-Initialize the Submodule

The most common fix — delete the submodule directory and let Git re-clone it:

bash
1# Remove the submodule directory
2rm -rf libs/mylib
3
4# Remove the submodule's Git metadata
5rm -rf .git/modules/libs/mylib
6
7# Re-initialize and update
8git submodule update --init --recursive

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

bash
1# Deinit the submodule (clears working tree and config)
2git submodule deinit -f libs/mylib
3
4# Remove cached metadata
5rm -rf .git/modules/libs/mylib
6
7# Re-initialize
8git submodule update --init libs/mylib

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):

bash
1# Check current URL in .gitmodules
2cat .gitmodules
3# [submodule "libs/mylib"]
4#     path = libs/mylib
5#     url = https://github.com/old-org/mylib.git  ← wrong
6
7# Update the URL
8git config --file .gitmodules submodule.libs/mylib.url https://github.com/new-org/mylib.git
9
10# Sync the URL to .git/config
11git submodule sync
12
13# Remove old cached clone and re-initialize
14rm -rf .git/modules/libs/mylib
15git submodule update --init libs/mylib

Fix 4: The Referenced Commit No Longer Exists

If the submodule's commit was force-pushed away or the branch was deleted:

bash
1# Check which commit the parent expects
2git ls-tree HEAD libs/mylib
3# 160000 commit abc1234... libs/mylib
4
5# Check if that commit exists in the submodule
6cd libs/mylib
7git cat-file -t abc1234
8# fatal: Not a valid object name abc1234  ← commit doesn't exist
9
10cd ../..
11
12# Option A: Update submodule to latest commit on its default branch
13cd libs/mylib
14git fetch origin
15git checkout origin/main
16cd ../..
17git add libs/mylib
18git commit -m "Update mylib submodule to latest main"
19
20# Option B: Point to a known good commit
21cd libs/mylib
22git checkout <known-good-commit>
23cd ../..
24git add libs/mylib
25git commit -m "Pin mylib to known-good commit"

Fix 5: Empty Submodule Directory

If the submodule directory exists but is empty (common after manual git clone without --recursive):

bash
1# Check if directory is empty
2ls libs/mylib/
3# (empty)
4
5# Remove it and let Git clone properly
6rmdir libs/mylib
7git submodule update --init --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

bash
1# Check submodule status
2git submodule status
3# -abc1234 libs/mylib  ← '-' prefix means not initialized
4# +def5678 libs/mylib  ← '+' means checked out at different commit
5#  abc1234 libs/mylib  ← space means OK
6
7# Check .gitmodules for URL and path
8git config --file .gitmodules --list
9
10# Check if the submodule directory is a valid Git repo
11cd libs/mylib && git status
12# fatal: not a git repository  ← this is the problem
13
14# Check cached submodule metadata
15ls .git/modules/

Preventing the Issue

bash
1# Always clone with --recursive to get submodules
2git clone --recursive https://github.com/org/repo.git
3
4# After pulling changes that add/modify submodules
5git pull
6git submodule update --init --recursive
7
8# Use a post-merge hook to auto-update submodules
9# .git/hooks/post-merge
10#!/bin/sh
11git submodule update --init --recursive

Common Pitfalls

  • Not removing .git/modules/ cache: Deleting the submodule directory without removing .git/modules/path/to/submodule leaves stale metadata. Git uses the cached (broken) clone instead of re-cloning. Always remove both the directory and the cache.
  • Cloning without --recursive: git clone does not fetch submodules by default. Running git submodule update --init after 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 .gitmodules updates the URL in the file but not in .git/config. Without git 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 update fails because the expected commit no longer exists. Pin submodules to tagged releases to avoid this.
  • Using relative paths in .gitmodules with 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 run git submodule update --init
  • If the URL changed, update .gitmodules and run git submodule sync
  • If the commit was force-pushed away, update the submodule to a new valid commit
  • Always clone with --recursive and run git submodule update --init --recursive after pulling
  • Check submodule status with git submodule status to diagnose which submodules are broken

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No 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 course
Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions