This Git error occurs when you try to delete, checkout, or reference a branch that doesn't exist in your repository. The branch may have been deleted, renamed, misspelled, or never fetched from the remote. Fix it by verifying the branch name and ensuring you've fetched the latest remote references.
The "branch not found" error occurs when Git cannot locate a branch with the name you specified. This typically happens when running commands like `git branch -d feature`, `git checkout feature`, or `git merge feature` where the branch name doesn't exist in your local repository. Git stores branch references as files in the `.git/refs/heads/` directory. When you reference a branch, Git looks for a matching file there. If the file doesn't exist, Git reports this error. The branch could be missing because: - It was never created locally - It was deleted - You're using the wrong name (case sensitivity matters) - The branch exists only on a remote and hasn't been fetched yet - The branch reference file is corrupted or contains hidden characters This error is common when switching between repositories, working with team members' branches, or after cleaning up stale branches.
First, check what branches actually exist in your repository:
# List all local branches
git branch
# List all remote tracking branches
git branch -r
# List all branches (local and remote)
git branch -a
# Search for branches matching a pattern (case-insensitive)
git branch -a | grep -i featureBranch names are case-sensitive on most systems. A branch named Feature is different from feature. Make sure you're using the exact spelling shown in the output.
If the branch exists on the remote but not locally, fetch it:
# Fetch all branches from all remotes
git fetch --all
# Fetch from a specific remote
git fetch origin
# Now list branches again
git branch -aAfter fetching, remote branches appear as remotes/origin/branch-name. You can then checkout or create a local tracking branch.
The branch might have been deleted from the remote. Verify by checking directly:
# List all refs on the remote (without fetching)
git ls-remote origin
# Look for a specific branch
git ls-remote origin | grep feature
# Update local refs and remove deleted branches
git fetch --pruneIf the branch doesn't appear in git ls-remote output, it was deleted from the remote. Check with your team or the repository's web interface to confirm.
Sometimes branch files can become corrupted or contain invisible characters. Inspect the refs directory:
# List local branch reference files
ls -la .git/refs/heads/
# Check for branches with unusual characters
ls .git/refs/heads/ | cat -A
# You can also check packed refs
cat .git/packed-refs | grep featureIf you see strange characters or the file appears corrupted, you may need to:
# Delete the corrupted reference file
rm .git/refs/heads/feature
# Then fetch fresh references
git fetch originIf you cloned with --single-branch, your repository only tracks one branch. Fix this:
# Check current fetch configuration
git config --get remote.origin.fetch
# If it shows a single branch like:
# +refs/heads/main:refs/remotes/origin/main
# Update to fetch all branches
git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
# Now fetch all branches
git fetch origin
# Verify branches are available
git branch -aThis is common in CI/CD environments that use shallow or single-branch clones.
Ensure your local repository has the latest reference information:
# Update all remote references
git remote update
# Or with pruning to remove stale refs
git remote update --prune
# You can also use
git fetch --all --pruneThis synchronizes your local knowledge of remote branches with what actually exists.
If you have multiple remotes, the branch might exist on a different remote:
# List all configured remotes
git remote -v
# Fetch from all remotes
git fetch --all
# Check which remote has the branch
git branch -r | grep feature
# The output shows which remote has the branch:
# origin/feature
# upstream/featureWhen checking out, specify the correct remote:
git checkout -b feature upstream/feature### How Git Stores Branch References
Git branches are simply pointers to commits, stored as files in .git/refs/heads/. Each file contains the SHA-1 hash of the commit the branch points to:
# View what a branch points to
cat .git/refs/heads/main
# Output: a1b2c3d4e5f6...
# Or use git commands
git rev-parse mainFor efficiency, Git also uses a packed-refs file that consolidates multiple refs into a single file:
cat .git/packed-refs### Case Sensitivity Across Platforms
Branch name case sensitivity depends on the filesystem:
| Platform | Default Filesystem | Case Sensitive |
|----------|-------------------|----------------|
| Linux | ext4 | Yes |
| macOS | APFS (default) | No |
| Windows | NTFS | No |
This can cause problems when:
- Team members use different operating systems
- A branch is created with one case, then recreated with different case
- You rename a branch changing only the case
To avoid issues:
- Use consistent lowercase naming conventions
- Avoid creating branches that differ only in case
### Recovering Deleted Branches
If a branch was accidentally deleted but you have committed to it:
# Find dangling commits
git fsck --lost-found
# Or use the reflog (if the branch was checked out recently)
git reflog | grep feature
# Recreate branch at a specific commit
git branch feature abc1234The reflog keeps records for about 90 days by default.
### CI/CD Branch Access
Many CI systems use optimized clones. To ensure branch access:
GitHub Actions:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch all historyGitLab CI:
variables:
GIT_DEPTH: 0
GIT_STRATEGY: clone### Branch Name Validation
Valid Git branch names:
- Cannot start with -
- Cannot contain .., ~, ^, :, ?, *, [, @{, \\
- Cannot end with / or .lock
- Cannot be a single @
Check if a name is valid:
git check-ref-format --branch "feature/new"warning: BOM detected in file, this may cause issues
UTF-8 Byte Order Mark (BOM) detected in file
fatal: Server does not support --shallow-exclude
Server does not support --shallow-exclude
warning: filtering out blobs larger than limit
Git partial clone filtering large blobs warning
fatal: Server does not support --shallow-since
Server does not support --shallow-since in Git
kex_exchange_identification: Connection closed by remote host
Connection closed by remote host when connecting to Git server