How to fix fatal: Needed a single revision in Git
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.
Quick fix
Fixes fatal: Needed a single revision
cat .gitmodules- Check which branch the submodule is configured to track
- Update the submodule branch in .gitmodules
- Remove --remote flag if not needed
- Fix shallow clone fetch restrictions
cat .gitmodulesfatal: Needed a single revision
What this error means
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.
Typical symptoms
- git submodule update --init fails with 'fatal: Needed a single revision'
- git submodule update --remote produces 'Unable to find current origin/master revision'
- CI/CD pipelines fail during the submodule checkout step
- git pull --recurse-submodules fails after parent repository updates
- The submodule directory exists but is empty or incomplete
Common causes
- Branch name mismatch: the submodule is configured to track 'master' but the remote only has 'main' (most common after GitHub's default branch rename)
- The tracked branch was deleted from the remote (e.g., feature branch merged and removed)
- Shallow clone restrictions: the parent repo was cloned with --depth and the submodule cannot fetch all branches
- SSH host key not accepted: Git cannot communicate with the submodule's remote server
- Corrupted submodule state: the initial checkout failed, leaving an empty directory that Git cannot recover from
- Path separator issues on Windows: the submodule's .git file uses backslashes instead of forward slashes
How to fix "fatal: Needed a single revision"
First, inspect your .gitmodules file to see the branch configuration:
cat .gitmodulesLook for entries like:
[submodule "path/to/submodule"]
path = path/to/submodule
url = git@github.com: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 git@github.com
# For GitLab
ssh -T git@gitlab.com
# For custom servers
ssh -T git@your-git-server.comAccept 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.
Advanced notes
### 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.
Frequently asked questions
- What does "fatal: Needed a single revision" mean?
- 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.
- How do I fix "fatal: Needed a single revision"?
- 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.
- What causes "fatal: Needed a single revision"?
- Branch name mismatch: the submodule is configured to track 'master' but the remote only has 'main' (most common after GitHub's default branch rename) The tracked branch was deleted from the remote (e.g., feature branch merged and removed) Shallow clone restrictions: the parent repo was cloned with --depth and the submodule cannot fetch all branches
- Is the "fatal: Needed a single revision" error serious?
- It is a medium-severity error โ more of a blocker than a risk. The steps above resolve it.
Official resources & further reading
- ๐Git Official Documentation
- ๐ฌ
- ๐ฌ
- ๐ฌ
- ๐ฌ
- ๐
- ๐
- ๐Git Submodule Documentation - Official Git Docs(Official Docs)