[Git] Error: fatal: pathspec 'filename' did not match any files — File not found or outside Git scope — How to Fix It
Summary
fatal: pathspec 'filename' did not match any files occurs when Git cannot find the file or directory you referenced in a command like git add or git commit. The most common causes are typos, wrong working directory, or the file being ignored or untracked.
Context
Git uses the term pathspec to refer to the file patterns you specify in commands. If the pathspec doesn’t match any tracked or untracked files within the repository, Git halts the operation and displays this error.
Probable Cause
- File path or name spelled incorrectly.
- File not yet created or saved.
- Running Git outside the repository root.
- File excluded by
.gitignoreor.git/info/exclude. - File exists in another branch or directory.
Quick Fix
- Check the repository status:
git statusThis shows tracked and untracked files currently visible to Git.
- Confirm the file exists:
ls # Linux/macOS dir # Windows - Check for case or spelling mismatch:
Remember:
File.txt≠file.txton case-sensitive systems. - If you’re in the wrong folder, navigate to the repo root:
cd /path/to/repo - Add the file explicitly:
git add path/to/filename - Check
.gitignoreif Git still can’t see it:Remove or comment out ignore patterns that exclude your file.
- Commit once the file is recognized:
git commit -m "Add file correctly"
Example
# Error
$ git add config.yaml
fatal: pathspec 'config.yaml' did not match any files
# Fix
$ ls
Config.yaml
$ git add Config.yaml
$ git commit -m "Add configuration file"
[main 1a2b3c4] Add configuration file
1 file changed, 12 insertions(+)
Alternate Scenarios
- Ignored files: The file matches an entry in
.gitignore. Temporarily comment it out to add it once. - Wrong branch: The file exists in another branch. Use
git switch branchnameto check. - Unsaved editor file: Ensure your IDE has written the file to disk.
Pitfalls & Debug
- Symptom → Git can’t find the file even though it exists.
Fix → Check working directory with
git rev-parse --show-toplevel. - Symptom → Ignored file won’t add.
Fix → Temporarily use
git add -f filenameto override.gitignore. - Symptom → File saved with wrong capitalization.
Fix → Rename it consistently using
mvorgit mv.
Verification & Next Steps
# Confirm you're in the repo
git rev-parse --show-toplevel
# Locate the file
find . -name filename
# List tracked files
git ls-files | grep filename
Best Practices
- Always verify the current directory before running
git add. - Use consistent casing across platforms.
- Keep
.gitignorepatterns well-defined and documented. - Regularly run
git statusto confirm file visibility.
Sources
Stack Overflow — pathspec did not match any files
Tool/Git, OS/Cross-platform, Topic/File Management