How to fix fatal: bad revision 'nonexistent' in Git

GitBEGINNERLOWUpdated Dec 6, 2025ยท 278 views

This error occurs when Git cannot resolve a revision specifier (branch name, tag, or commit hash) that you provided. The reference either doesn't exist, hasn't been fetched, or was typed incorrectly.

Quick fix

Fixes fatal: bad revision 'nonexistent'

# List all local branches
git branch -a

# List all tags
git tag -l

# Check if a specific commit exists
git cat-file -t <commit-hash>
  1. Verify the reference exists locally
  2. Fetch the latest references from remote
  3. Check for typos in the reference name
  4. Handle shallow clone issues
git โ€” error
# List all local branches
git branch -a
# List all tags
git tag -l
# Check if a specific commit exists
git cat-file -t <commit-hash>
fatal: bad revision 'nonexistent'

What this error means

When you run a Git command that requires a revision (like `git log`, `git diff`, `git checkout`, or `git revert`), Git attempts to resolve the argument you provided into a specific commit SHA. A "revision" in Git terminology can be a branch name, tag name, commit hash, or special reference like HEAD. The "fatal: bad revision" error means Git could not find any object matching what you specified. This typically happens when you reference a branch that doesn't exist locally, use a commit hash that isn't in your repository, or make a typo in a branch or tag name. Git doesn't distinguish between "doesn't exist" and "can't be resolved to a commit" in this error message. This error is particularly common in CI/CD environments where shallow clones are used, after force-pushes that rewrite history, or when working with remote branches that haven't been fetched yet.

Typical symptoms

  • Git commands fail with 'fatal: bad revision' followed by the problematic reference
  • The error appears when using git log, git diff, git checkout, git revert, or git cherry-pick
  • Commands work with some branches but fail with others
  • CI/CD pipelines fail when referencing commits or branches
  • The error mentions a specific branch name, tag, or commit hash that you expected to exist

Common causes

  1. The branch or tag name doesn't exist (typo or deleted branch)
  2. The reference exists on remote but hasn't been fetched locally
  3. Using a shallow clone that doesn't contain the referenced commit
  4. The commit was removed by a force-push or rebase operation
  5. Using 'origin' instead of 'origin/branch-name' in comparisons
  6. Empty repository with no commits (HEAD doesn't exist yet)

How to fix "fatal: bad revision 'nonexistent'"

1Verify the reference exists locally

First, check if the branch, tag, or commit actually exists in your local repository:

bash
# List all local branches
git branch -a

# List all tags
git tag -l

# Check if a specific commit exists
git cat-file -t <commit-hash>

If the reference is a remote branch (like origin/feature-branch), verify it appears in the list of remote-tracking branches.

2Fetch the latest references from remote

If the branch or tag exists on the remote but not locally, fetch it:

bash
# Fetch all branches and tags from all remotes
git fetch --all

# Fetch with tags explicitly
git fetch --all --tags

# Fetch a specific branch
git fetch origin feature-branch

After fetching, retry your command. The reference should now be available locally.

3Check for typos in the reference name

The error often occurs due to simple typos. Verify your reference name:

bash
# Search for similar branch names
git branch -a | grep -i "partial-name"

# Search for similar tags
git tag -l | grep -i "partial-name"

# If using a commit hash, verify it's complete (at least 7 characters)
git rev-parse --verify <commit-hash>

Common mistakes include:
- Using origin instead of origin/main
- Confusing branch names with tag names
- Missing characters in commit hashes

4Handle shallow clone issues

If you're working with a shallow clone (common in CI/CD), you may not have the full commit history:

bash
# Check if repository is shallow
git rev-parse --is-shallow-repository

# Convert shallow clone to full clone
git fetch --unshallow

# Or fetch more history incrementally
git fetch --deepen=100

For CI/CD configurations, increase the fetch depth:

GitHub Actions:

yaml
- uses: actions/checkout@v4
  with:
    fetch-depth: 0  # Full history

GitLab CI:

yaml
variables:
  GIT_DEPTH: 0
5Handle empty or new repositories

In a new repository with no commits, HEAD doesn't exist yet:

bash
# Check if HEAD exists
git rev-parse HEAD 2>/dev/null || echo "No commits yet"

# For new repositories, create an initial commit
git commit --allow-empty -m "Initial commit"

This is normal for freshly initialized repositories before the first commit.

6Use the correct remote reference syntax

When comparing with remote branches, use the full reference:

bash
# Wrong - 'origin' is a remote, not a revision
git diff origin

# Correct - use the full remote/branch reference
git diff origin/main

# Also correct - use HEAD on remote
git diff origin/HEAD

To see what origin/HEAD points to:

bash
git symbolic-ref refs/remotes/origin/HEAD
7Recover references after force-push

If a commit was lost due to a force-push, you may be able to recover it:

bash
# Check the reflog for lost commits
git reflog

# Search for the commit in reflog
git reflog | grep "commit message fragment"

# Once found, you can checkout or cherry-pick it
git checkout <recovered-hash>

Note: Reflog entries expire after 90 days by default. For remote repositories, you may need to contact your repository host for assistance.

Advanced notes

### Git Revision Syntax

Git supports many ways to specify revisions beyond simple branch names:

- HEAD~2: Two commits before HEAD (first parent only)
- HEAD^2: Second parent of HEAD (useful for merge commits)
- @{yesterday}: Where HEAD was yesterday
- main@{upstream}: The upstream of main branch
- :/fix bug: Most recent commit matching "fix bug" in message

All of these can produce "bad revision" errors if the referenced commit doesn't exist.

### Detached HEAD State

When in detached HEAD state, some revision shortcuts may not work as expected:

bash
# Check if in detached HEAD state
git symbolic-ref HEAD 2>/dev/null || echo "Detached HEAD"

### Jenkins and CI/CD Environments

Jenkins and similar tools sometimes check out specific commits rather than branches. Use explicit ref specs:

bash
# Instead of branch name, use full ref
git checkout refs/remotes/origin/main

# Or use commit SHA directly
git checkout ${GIT_COMMIT}

### Git Worktrees

If using git worktrees, ensure you're in the correct worktree:

bash
# List all worktrees
git worktree list

# Each worktree may have different branches checked out

Frequently asked questions

What does "fatal: bad revision 'nonexistent'" mean?
When you run a Git command that requires a revision (like git log, git diff, git checkout, or git revert), Git attempts to resolve the argument you provided into a specific commit SHA. A "revision" in Git terminology can be a branch name, tag name, commit hash, or special reference like HEAD. The "fatal: bad revision" error means Git could not find any object matching what you specified.
How do I fix "fatal: bad revision 'nonexistent'"?
This error occurs when Git cannot resolve a revision specifier (branch name, tag, or commit hash) that you provided. The reference either doesn't exist, hasn't been fetched, or was typed incorrectly.
What causes "fatal: bad revision 'nonexistent'"?
The branch or tag name doesn't exist (typo or deleted branch) The reference exists on remote but hasn't been fetched locally Using a shallow clone that doesn't contain the referenced commit
Is the "fatal: bad revision 'nonexistent'" error serious?
It is a low-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading