This error occurs when Git cannot resolve a specific commit or branch in a submodule. It typically happens when the branch configured for the submodule no longer exists, or when shallow clone restrictions prevent fetching the required revision.
The "fatal: Needed a single revision" error appears when Git attempts to update or initialize a submodule but cannot find the commit or branch it's supposed to track. Git submodules work by pointing to a specific commit in another repository, and this error indicates that the target commit or branch cannot be resolved. This error is often accompanied by a follow-up message like "Unable to find current origin/master revision in submodule path" which provides more context about what Git was looking for. The root cause is usually a mismatch between what the parent repository expects (recorded in .gitmodules and the submodule reference) and what actually exists in the submodule's remote repository. Common scenarios that trigger this error include: the submodule's default branch was renamed from "master" to "main", a feature branch that was being tracked was deleted after merging, the submodule repository was cloned shallowly and the required branch isn't available, or network/SSH issues are preventing Git from communicating with the submodule's remote server.
First, inspect your .gitmodules file to see the branch configuration:
cat .gitmodulesLook for entries like:
[submodule "path/to/submodule"]
path = path/to/submodule
url = [email protected]:org/repo.git
branch = masterIf branch = master but the remote now uses main, this is likely your issue.
If the branch name has changed (e.g., master to main), update .gitmodules:
git config -f .gitmodules submodule.path/to/submodule.branch mainOr edit .gitmodules directly and change branch = master to branch = main.
After updating, sync the configuration:
git submodule sync
git submodule update --init --recursiveThe --remote flag tells Git to fetch the latest commit from the configured branch, which requires a valid branch configuration. If you just want to checkout the commit recorded in the parent repository:
# Instead of:
git submodule update --init --remote
# Use:
git submodule update --init --recursiveThis checks out the exact commit the parent repository expects, without needing branch configuration.
If your repository was cloned with --depth, submodules may be restricted to fetching only specific branches. Check and fix the fetch configuration:
# Check current fetch configuration
git submodule foreach git config --get remote.origin.fetch
# Fix to fetch all branches
git submodule foreach git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'
# Now update submodules
git submodule update --init --recursiveIf the submodule is in a corrupted state (empty directory, incomplete checkout), remove it completely and start fresh:
# Remove the submodule directory
rm -rf path/to/submodule
# Remove the submodule's Git data
rm -rf .git/modules/path/to/submodule
# Reinitialize
git submodule update --init --recursiveThis forces a clean checkout of the submodule.
Git may fail silently if it cannot verify the SSH host key for the submodule's server. Test connectivity manually:
# For GitHub
ssh -T [email protected]
# For GitLab
ssh -T [email protected]
# For custom servers
ssh -T [email protected]Accept the host key when prompted, then retry the submodule update.
Sometimes running a manual pull inside the submodule can unblock the situation:
cd path/to/submodule
git fetch origin
git checkout main # or the correct branch name
git pull origin main
cd ..
# Now try the submodule update again
git submodule update --init --recursiveSome older Git versions have bugs with submodule handling. Updating Git has resolved this issue for many users:
# Ubuntu/Debian
sudo apt update && sudo apt install git
# macOS with Homebrew
brew upgrade git
# Windows - download latest from git-scm.com
# Verify version
git --versionGit 2.38+ has significant improvements to submodule handling.
### CI/CD Pipeline Considerations
When this error occurs in CI/CD (Jenkins, GitLab CI, GitHub Actions), the shallow clone is often the culprit. Configure your CI to use a deeper clone:
GitHub Actions:
- uses: actions/checkout@v4
with:
submodules: recursive
fetch-depth: 0 # Full clone, not shallowGitLab CI:
variables:
GIT_SUBMODULE_STRATEGY: recursive
GIT_DEPTH: 0 # Full cloneJenkins:
checkout([
$class: 'GitSCM',
extensions: [[$class: 'CloneOption', depth: 0, shallow: false]],
submoduleCfg: [[recursiveSubmodules: true]]
])### Windows Path Separator Issue
On Windows or in cross-platform repositories, the submodule's .git file may have incorrect path separators. Check the file:
cat path/to/submodule/.gitIf it shows backslashes like gitdir: ..\.git\modules\submodule, change them to forward slashes: gitdir: ../.git/modules/submodule
### Detached HEAD State
Submodules normally operate in a "detached HEAD" state, pointing to a specific commit. If you need to make changes in a submodule:
cd path/to/submodule
git checkout main # or your working branch
# make changes
git commit -m "Your changes"
git push
# Back in parent repo, update the submodule reference
cd ..
git add path/to/submodule
git commit -m "Update submodule reference"### Recursive Submodules
If your submodules have their own submodules, ensure you use --recursive:
git submodule update --init --recursiveWithout --recursive, nested submodules won't be initialized.
kex_exchange_identification: Connection closed by remote host
Connection closed by remote host when connecting to Git server
fatal: unable to access: Proxy auto-configuration failed
How to fix 'Proxy auto-configuration failed' in Git
fatal: unable to access: Authentication failed (proxy requires basic auth)
How to fix 'Authentication failed (proxy requires basic auth)' in Git
fatal: unable to access: no_proxy configuration not working
How to fix 'no_proxy configuration not working' in Git
fatal: unable to read tree object in treeless clone
How to fix 'unable to read tree object in treeless clone' in Git