Git, cannot checkout branch - error, pathspec '...' did not match any file(s) known to git
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The error error: pathspec 'branch-name' did not match any file(s) known to git means Git cannot find a branch, tag, or file matching the name you provided. This almost always falls into one of four causes: a typo, a missing git fetch, a remote-only branch that needs explicit tracking, or a case-sensitivity mismatch. Here is how to diagnose and fix each one.
Understanding What the Error Means
When you run git checkout branch-name (or git switch branch-name), Git searches in this order:
- Local branches
- Remote-tracking branches (to auto-create a local tracking branch)
- Tags
- Files or paths in the working tree
If nothing matches, Git reports the pathspec error. The message says "file(s)" because git checkout historically handled both branch switching and file restoration.
Cause 1: The Branch Has Not Been Fetched
This is the most common cause, especially in team environments. A colleague created a branch, pushed it, and you try to check it out. But your local repository does not know about it yet.
Fix:
If you only want to fetch from a specific remote:
Verifying the Branch Exists on the Remote
If the branch does not appear in git branch -r even after fetching, it either does not exist on the remote or was deleted.
Cause 2: Typo or Case Mismatch
Git branch names are case-sensitive. Feature/Login, feature/login, and feature/Login are three different branches.
Fix:
Then use the exact name:
Using Tab Completion
Most shells support tab completion for Git branch names. Type git checkout feat and press Tab to see matching branches. This avoids typos entirely.
Cause 3: Remote-Only Branch with Multiple Remotes
When you have multiple remotes (e.g., origin and upstream), Git's automatic tracking branch creation can fail because it does not know which remote to use.
Fix:
This creates a local branch feature/payment that tracks origin/feature/payment.
Cause 4: Shallow Clone Missing the Branch
If you cloned with --depth 1 or --single-branch, your repository may not have all branch references.
Fix:
Cause 5: Branch Was Deleted
The branch may have existed at one point but was deleted from the remote (e.g., after a pull request was merged).
Verify:
If the branch no longer appears, it was deleted. You can check with the hosting platform (GitHub, GitLab) whether the branch still exists.
Diagnostic Flowchart
Follow these steps in order to resolve the error:
git checkout vs git switch
Since Git 2.23 (August 2019), the recommended command for switching branches is git switch, which separates branch-switching from file-restoration:
| Action | Old Command | New Command |
| Switch to existing branch | git checkout branch | git switch branch |
| Create and switch | git checkout -b branch | git switch -c branch |
| Create tracking branch | git checkout -b branch origin/branch | git switch -c branch origin/branch |
| Restore a file | git checkout -- file | git restore file |
The error message is the same with git switch, but using the newer commands makes intent clearer and avoids accidental file overwrites.
Common Pitfalls
- Running
git checkoutbeforegit fetch. Your local repository only knows about remote branches after fetching. Always fetch first. - Copy-pasting branch names from Slack or Jira. Chat tools and project trackers sometimes convert hyphens, add invisible Unicode characters, or change capitalization. Type the branch name manually or use tab completion.
- Confusing
origin/branchwithbranch.origin/feature-xis a remote-tracking reference, not a local branch. Runninggit checkout origin/feature-xputs you in a detached HEAD state. Instead, rungit checkout feature-x(without theorigin/prefix) to create a local tracking branch. - Stale remote-tracking references. If a branch was renamed or deleted on the remote, your local
git branch -routput may still show the old name. Rungit fetch --pruneto clean up. - NTFS or FAT32 case-insensitivity. On Windows, the filesystem is case-insensitive, which can cause Git to conflate branches like
feature/APIandfeature/api. Stick to lowercase branch names on Windows.
Summary
- The pathspec error means Git cannot find a matching branch, tag, or file.
- Run
git fetch --allfirst. This fixes the problem in most cases. - Use
git branch -ato list all branches and verify the exact name (including case). - For multiple remotes, explicitly create a tracking branch with
git checkout -b name origin/name. - For shallow clones, run
git fetch --unshallowto retrieve all branch references. - Prefer
git switchovergit checkoutfor branch switching to avoid ambiguity between branches and files.

