pathspec did not match any files
This error occurs when you specify a file path or pattern in a Git command that does not exist in the working directory or is not tracked by Git.
Usually happens because:
- ☑ Spelling typo inside target filename or path string
- ☑ Target file is new and has never been tracked by Git
- ☑ Active directory terminal does not match relative path prefix
🔍 Quick Checklist:
What is pathspec did not match any files?
This error is returned by the Git CLI when a path parameter (called a 'pathspec') provided to a command (like git checkout, git add, git restore, or git reset) cannot be resolved. Git throws this warning because the filename is misspelled, the path is relative to the wrong directory, or the file exists in the directory but has not been tracked or added to Git's index yet (especially during checkout/restore commands).
Common Causes
- Spelling typo in file name or path: Typo in the file name or folder structure specified in the command.
- File not tracked by Git: Attempting to run checkout or restore on a new file that has never been staged or committed.
- Relative path mismatch: Your terminal is in a subdirectory, and you specified a path relative to the project root without adjusting the prefix.
| Cause | Frequency |
|---|---|
| Spelling typo in filename or path specification | ⭐⭐⭐⭐⭐ |
| Target file is untracked or not in index | ⭐⭐⭐⭐ |
| Incorrect relative path mapping from active terminal | ⭐⭐⭐ |
Common Mistakes
- Attempting to discard changes of a brand new file using `git checkout <file>`. Because the file was never tracked, Git cannot restore it. You must delete the file manually.
- Forgetting that Git paths are case-sensitive, even on Windows systems where the host operating system path resolution is case-insensitive.
How to Fix
Git Operations & Verification
If you get a pathspec error when checking out or restoring a file, make sure it has been tracked by adding it to index first.
# 1. Track the file
$ git add config.json
# 2. Reset or check out now succeeds
$ git reset HEAD config.jsonPlatform Specific Fixes
Find files inside terminal using standard search commands.
find . -name "config.json"
# Or list matching files
ls -l src/Best Practices
- Always use the `Tab` key to autocomplete file paths inside your terminal to prevent typos.
- Run `git status` beforehand to verify which files are staged, modified, or untracked.
Frequently Asked Questions (FAQ)
Q: What is a pathspec in Git?
A pathspec is a path pattern syntax Git uses to limit commands to a specific subset of files (e.g. 'src/*.js' or 'index.html').
Q: Why does 'git checkout' throw pathspec did not match?
If you run 'git checkout filename' to discard changes, but the file is new and untracked, Git has no record of it in its database and rejects the command. You should delete the untracked file manually instead.
Q: How do I fix path issues on Windows?
Git commands internally use forward slashes (/) for paths. If you copy Windows backslashes (\), it can result in a pathspec mismatch in some shells.
Q: How do I add all files without naming them?
Run 'git add .' to stage all modified and untracked files in the current folder recursively.