git checkout: pathspec did not match
Quick summary
You told git checkout to switch to a file or branch that does not exist — git reports 'pathspec did not match any file(s) known to git'.
Why this happens
The branch name is misspelled or the branch was never created on this repository or remote
The file path you are trying to checkout does not exist in the current commit tree
You forgot to fetch remote branches before trying to checkout a remote-only branch locally
Minimal example
git checkout feature/login # error: pathspec 'feature/login' did not matchgit fetch origin
git checkout -b feature/login origin/feature/login # creates local tracking branchFetching first updates remote tracking refs, then you can create a local branch that tracks the remote branch.
How to diagnose
Run git branch -a to list all local and remote branches and verify the exact branch name
Run git ls-files to check whether the file path exists in the repository index
Run git fetch --all to update remote tracking branches before retrying the checkout
How to fix
Correct the branch name spelling or create the branch first with git branch or git checkout -b
Fetch remote branches with git fetch origin then checkout with git checkout -b local-name origin/remote-name
Use git checkout <commit> -- <file> to restore a file from a specific commit if it existed historically
How to prevent
Use git branch -a regularly to see available branches and avoid typos in branch names
Set up branch auto-completion in your shell to prevent misspelling branch names
Related Resources
Related Glossary
Related Lessons
- git checkout
Learn how to switch branches and restore files.