This error occurs when Git cannot find a file or directory matching the path you specified. Common causes include typos, wrong working directory, case sensitivity issues, or files excluded by .gitignore.
The "fatal: pathspec did not match any files" error is Git's way of telling you that the file or directory path you specified doesn't exist or isn't accessible in the current context. A pathspec in Git is the pattern used to specify which files or directories a command should operate on. When you run commands like `git add file.txt`, `git checkout feature`, or `git rm config.json`, Git searches for matching files. If nothing matches your pathspec, Git raises this error. This error commonly appears with `git add`, `git rm`, `git checkout`, `git commit`, and `git restore` commands. It's a user-facing error indicating the specified target doesn't exist in the repository or working directory, rather than a system or configuration problem.
First, confirm the file exists in your filesystem. Use ls (Linux/macOS) or dir (Windows) to check:
# Linux/macOS
ls -la file.txt
# Windows
dir file.txtIf the file doesn't exist, you'll need to create it first before adding it to Git. Git's add command stages existing files - it doesn't create new ones.
Make sure you're in the correct directory relative to the file you want to add:
# Show current directory
pwd
# List files in current directory
ls -la
# If the file is in a subdirectory, use the full relative path
git add src/components/file.txtYou can also run git status to see which files Git detects as new or modified - this shows paths relative to the repository root.
Git is case-sensitive. A file named "README.md" won't match "readme.md". Use tab completion to ensure accuracy:
# Type partial name and press Tab to autocomplete
git add READ<Tab>
# Or list files to check exact names
ls -la | grep -i readmeOn Windows/macOS, the filesystem may be case-insensitive, but Git still tracks the case. If you need to fix case sensitivity:
# Rename using a temporary name
git mv README.md temp.md
git mv temp.md readme.mdIf a file matches a pattern in .gitignore, Git may not see it for certain operations:
# Check if a file is ignored
git check-ignore -v file.txt
# If you want to force-add an ignored file
git add -f file.txtNote: If you're getting this error with git rm on an ignored file, it's because Git isn't tracking it in the first place.
The git status command shows the current state of your working directory:
git statusLook for your file under:
- Untracked files: New files not yet added to Git
- Changes not staged for commit: Modified tracked files
- Changes to be committed: Already staged files
If your file doesn't appear anywhere in the output, it either doesn't exist or is ignored.
If you see this error when checking out a branch, the branch may exist on the remote but not locally:
# Fetch all remote branches
git fetch --all
# Now try checking out the branch
git checkout feature-branch
# Or create a local tracking branch explicitly
git checkout -b feature-branch origin/feature-branchThis is common when a colleague created a new branch after you cloned the repository.
When using wildcards (glob patterns), your shell may expand them before Git sees them:
# Wrong - shell expands *.txt before Git
git add *.txt
# Better - quote to let Git handle the pattern
git add '*.txt'
# Or escape the asterisk
git add \*.txtIf no files match the pattern, you'll get the pathspec error. Use ls *.txt first to verify matches.
Understanding Git's pathspec syntax:
Git pathspecs support advanced patterns beyond simple file paths. The official documentation at git-scm.com/docs/gitglossary explains the full syntax.
Key pathspec features:
- :(top) or :!/ - Match from repository root regardless of current directory
- :(icase) - Case-insensitive matching
- :(glob) - Enable glob pattern matching
- :(literal) - Treat wildcards as literal characters
- :(exclude) or :! - Exclude matching paths
Example of case-insensitive add:
git add ':(icase)readme.md'Windows-specific issues:
On Windows, path length limits (260 characters by default) can cause this error. If you have deeply nested directories, try:
git config core.longpaths trueCI/CD pipelines:
In automated environments, always verify files exist before git operations:
if [ -f "file.txt" ]; then
git add file.txt
fiwarning: BOM detected in file, this may cause issues
UTF-8 Byte Order Mark (BOM) detected in file
fatal: Server does not support --shallow-exclude
Server does not support --shallow-exclude
warning: filtering out blobs larger than limit
Git partial clone filtering large blobs warning
fatal: Server does not support --shallow-since
Server does not support --shallow-since in Git
kex_exchange_identification: Connection closed by remote host
Connection closed by remote host when connecting to Git server