How to fix fatal: invalid reference: branch-name in Git

GitBEGINNERLOWUpdated Dec 6, 2025ยท 285 views

The 'fatal: invalid reference' error occurs when Git cannot find the branch, tag, or commit you specified. This typically happens due to typos, case sensitivity issues, or when the remote branch hasn't been fetched yet.

Quick fix

Fixes fatal: invalid reference: branch-name

# List all local branches
git branch

# List all branches including remote-tracking branches
git branch -a

# List branches with more details
git branch -vv
  1. List available branches to verify the correct name
  2. Fetch remote branches
  3. Check for case sensitivity issues
  4. Quote branch names with special characters
git โ€” error
# List all local branches
git branch
# List all branches including remote-tracking branches
git branch -a
# List branches with more details
git branch -vv
fatal: invalid reference: branch-name

What this error means

This error occurs when Git cannot locate the reference (branch, tag, or commit) you're trying to check out. In Git terminology, a "reference" is any pointer to a commit - most commonly a branch name. When you see "fatal: invalid reference: branch-name", Git is telling you that it searched its list of local branches, remote tracking branches, tags, and other refs, but couldn't find anything matching that name. About 90% of the time, this error simply means "this branch doesn't exist." The reference could be misspelled, using incorrect casing (Git is case-sensitive), or the remote branch may not have been fetched to your local repository yet. This is one of the most common Git errors, especially when working with multiple remotes, switching between repositories, or trying to check out branches from remote repositories without fetching first.

Typical symptoms

  • git checkout command fails with 'fatal: invalid reference'
  • git switch command reports the branch doesn't exist
  • Unable to check out a branch that exists on the remote
  • Branch name appears correct but Git still reports it as invalid
  • Error occurs when using branch names with special characters or spaces
  • CI/CD pipeline fails when checking out specific branches
  • Tab completion for branch names doesn't show the expected branch
  • git branch -a doesn't list the expected remote branch

Common causes

  1. Typo in the branch name (most common cause)
  2. Case mismatch - Git branch names are case-sensitive
  3. Remote branch not fetched - the branch exists on the server but not locally
  4. Branch was deleted on the remote but local refs weren't updated
  5. Invalid characters in the branch name (spaces, special chars)
  6. Branch name conflicts with shell interpretation (brackets, asterisks)
  7. Stale remote-tracking references after repository history rewrite
  8. Branch name starts with a hyphen or is named 'HEAD' (reserved)

How to fix "fatal: invalid reference: branch-name"

1List available branches to verify the correct name

First, check which branches actually exist in your repository:

bash
# List all local branches
git branch

# List all branches including remote-tracking branches
git branch -a

# List branches with more details
git branch -vv

Look for the branch you're trying to check out. Pay close attention to:
- Exact spelling
- Capitalization (feature-branch vs Feature-Branch)
- Prefix (origin/main vs main)

If the branch isn't listed, it may need to be fetched from the remote.

2Fetch remote branches

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

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

# Fetch from a specific remote
git fetch origin

# Fetch and prune deleted remote branches
git fetch --prune

After fetching, list branches again to confirm the branch is now available:

bash
git branch -a | grep branch-name

Now try checking out the branch:

bash
# Checkout and track the remote branch
git checkout branch-name

# Or explicitly create a local branch tracking the remote
git checkout -b branch-name origin/branch-name
3Check for case sensitivity issues

Git branch names are case-sensitive on most systems. A common issue is trying to checkout a branch with incorrect casing:

bash
# These are different branches in Git:
git checkout feature-branch    # lowercase
git checkout Feature-Branch    # mixed case
git checkout FEATURE-BRANCH    # uppercase

To find the correct case, search for the branch:

bash
# Case-insensitive search for branches
git branch -a | grep -i "branch-name"

# Show all references containing the name
git show-ref | grep -i "branch-name"

Note: On case-insensitive file systems (Windows, macOS default), Git may have issues with branches that differ only by case. Avoid creating branches with case-only differences.

4Quote branch names with special characters

If your branch name contains special characters, the shell might interpret them before Git sees them:

bash
# Branch names with brackets need quoting (especially in zsh)
git checkout "feature/[JIRA-123]"

# Branch names with spaces (not recommended but possible)
git checkout "feature with spaces"

# Use single quotes to prevent variable expansion
git checkout 'feature/$variable'

Common shell interpretation issues:
- [...] in zsh triggers glob pattern matching
- * and ? are wildcard characters
- $ triggers variable expansion
- Spaces split the argument into multiple parts

Best practice: Use simple branch names with only letters, numbers, hyphens, and forward slashes.

5Verify the branch exists before checkout

You can explicitly verify a reference exists before attempting checkout:

bash
# Check if the reference exists
git show-ref --verify --quiet refs/heads/branch-name && echo "Branch exists" || echo "Branch not found"

# Check for remote branches
git show-ref --verify --quiet refs/remotes/origin/branch-name && echo "Remote branch exists" || echo "Not found"

# List all references to find the correct name
git show-ref

If the branch doesn't exist, you can create it:

bash
# Create a new branch at current HEAD
git checkout -b branch-name

# Create a new branch from a specific commit
git checkout -b branch-name commit-hash

# Create a branch tracking a remote branch
git checkout -b branch-name origin/branch-name
6Handle deleted remote branches

If a branch was deleted on the remote, your local repository might still have stale references:

bash
# Prune stale remote-tracking branches
git fetch --prune

# Or prune a specific remote
git remote prune origin

# Enable auto-pruning for future fetches
git config --global fetch.prune true

If you have a local branch tracking a deleted remote branch:

bash
# Delete the local branch
git branch -d branch-name

# Force delete if it has unmerged changes
git branch -D branch-name
7Reset repository references if corrupted

If none of the above works and you suspect repository corruption:

bash
# Check repository for issues
git fsck

# Remove and re-add the remote
git remote remove origin
git remote add origin git@github.com:user/repo.git
git fetch origin

# Refresh all remote-tracking branches
git remote update origin --prune

As a last resort, you can re-clone the repository:

bash
# Move current repo and clone fresh
mv myrepo myrepo.backup
git clone git@github.com:user/repo.git
cd myrepo

# Copy any local uncommitted changes from backup if needed

Advanced notes

### Understanding Git References

A Git reference (ref) is a pointer to a commit. References include:
- Branches (refs/heads/main)
- Remote-tracking branches (refs/remotes/origin/main)
- Tags (refs/tags/v1.0.0)
- HEAD (current branch pointer)
- Stash (refs/stash)

When you run git checkout branch-name, Git searches for a matching reference in this order:
1. Local branch (refs/heads/branch-name)
2. Remote-tracking branch (refs/remotes/*/branch-name)
3. Tag (refs/tags/branch-name)
4. Abbreviated commit SHA

### Invalid Branch Name Characters

Git has strict rules for branch names. Invalid names include:
- Starting with a hyphen: -feature
- Containing ..: feature..test
- Containing @{: feature@{test}
- Ending with .lock: feature.lock
- Containing whitespace or control characters
- Using reserved names like HEAD

Check if a name is valid:

bash
git check-ref-format --branch "proposed-name"

### Using git switch (Modern Alternative)

Git 2.23+ introduced git switch as a cleaner alternative to git checkout for branch operations:

bash
# Switch to existing branch
git switch branch-name

# Create and switch to new branch
git switch -c new-branch

# Create branch tracking remote
git switch -c branch-name --track origin/branch-name

### Checkout vs Switch vs Restore

| Command | Purpose |
|---------|---------|
| git switch | Switch branches |
| git restore | Restore file contents |
| git checkout | Both (legacy, still works) |

### Dealing with Detached HEAD

If you accidentally check out a commit instead of a branch:

bash
# Create a branch at current detached HEAD
git checkout -b new-branch-name

# Or return to a branch
git checkout main

### Case Sensitivity on Different Systems

| OS | File System | Git Branch Case |
|----|-------------|-----------------|
| Linux | ext4 | Case-sensitive |
| macOS | APFS (default) | Case-insensitive |
| Windows | NTFS | Case-insensitive |

On case-insensitive systems, Git might get confused if you have branches like Feature and feature. Run git pack-refs --all to normalize references.

### CI/CD Considerations

In CI/CD pipelines, checkout errors often occur because:
- The branch hasn't propagated yet (add a small delay)
- Shallow clones don't include the branch
- Protected branches require authentication

bash
# Fetch with full depth if shallow clone is causing issues
git fetch --unshallow

# Fetch specific branch explicitly
git fetch origin branch-name:refs/remotes/origin/branch-name

Frequently asked questions

What does "fatal: invalid reference: branch-name" mean?
This error occurs when Git cannot locate the reference (branch, tag, or commit) you're trying to check out. In Git terminology, a "reference" is any pointer to a commit - most commonly a branch name. When you see "fatal: invalid reference: branch-name", Git is telling you that it searched its list of local branches, remote tracking branches, tags, and other refs, but couldn't find anything matching that name.
How do I fix "fatal: invalid reference: branch-name"?
The 'fatal: invalid reference' error occurs when Git cannot find the branch, tag, or commit you specified. This typically happens due to typos, case sensitivity issues, or when the remote branch hasn't been fetched yet.
What causes "fatal: invalid reference: branch-name"?
Typo in the branch name (most common cause) Case mismatch - Git branch names are case-sensitive Remote branch not fetched - the branch exists on the server but not locally
Is the "fatal: invalid reference: branch-name" error serious?
It is a low-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading