error: pathspec 'X' did not match any file(s) known to git
ID: git/pathspec-no-match
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 2 | active | — | — | — |
Root Cause
This error occurs when git cannot find a file, directory, or branch matching the name you specified. Common causes include typos in file/branch names, forgetting to fetch remote branches before checkout, or running git commands from the wrong directory. The pathspec argument is matched against tracked files and known refs — if neither matches, git reports this error.
genericWorkarounds
-
92% success Check spelling and use git ls-files or git branch -a to verify the name
Check spelling and use git ls-files or git branch -a to verify the name
-
88% success Fetch remote branches if switching to a remote branch
git fetch origin && git checkout branch-name
Sources: https://git-scm.com/docs/git-fetch
Dead Ends
Common approaches that don't work:
-
Create the file manually then checkout
65% fail
git checkout looks for the path in the committed tree objects, not the working directory. Creating a file on disk does not add it to git's object database. You need to first stage and commit the file, or verify the correct spelling of an existing tracked file.
-
Use git checkout -f
72% fail
The -f (force) flag in git checkout overwrites local modifications, but it cannot conjure a path that does not exist in the index or any commit. If the pathspec has no match in git's tracked files or refs, -f has no effect on the outcome.