How to fix fatal: pathspec 'file.txt' did not match any files in Git

GitBEGINNERLOWUpdated Dec 6, 2025· 226 views

This error occurs when you try to use git rm on a file that Git is not tracking. The file may have already been deleted, never been added to Git, or you may have a typo in the filename.

Quick fix

Fixes fatal: pathspec 'file.txt' did not match any files

git ls-files | grep filename
  1. Check if the file is tracked by Git
  2. If the file was already deleted with rm
  3. If the file was never tracked
  4. Check for case sensitivity issues
git — error
git ls-files | grep filename
fatal: pathspec 'file.txt' did not match any files

What this error means

When you run `git rm`, Git looks for the specified file in its index (the staging area). If the file is not being tracked by Git—meaning it was never added with `git add` or has already been removed from the index—Git cannot remove it and shows this error. Unlike the regular `rm` command which deletes files from your filesystem, `git rm` is specifically designed to remove tracked files from both your working directory and Git's index in a single operation. If Git doesn't know about a file, it has nothing to remove. This error commonly appears when you've already deleted a file using the regular `rm` command (or your file browser), when you're trying to remove a file that was never committed, or when there's a mismatch between what you typed and the actual filename (including case sensitivity on Linux/macOS).

Typical symptoms

  • Running git rm on a file produces 'fatal: pathspec did not match any files'
  • The file exists on disk but git rm fails to remove it
  • You deleted a file with rm and now git rm won't work on it
  • git status shows the file as untracked or doesn't show it at all
  • Trying to remove multiple files with a pattern fails completely

Common causes

  1. The file was already deleted from the filesystem using rm (not git rm)
  2. The file was never tracked by Git (never ran git add on it)
  3. Typo or misspelling in the filename
  4. Case sensitivity mismatch (Linux/macOS filesystems are case-sensitive)
  5. The file was already removed in a previous commit
  6. Trying to remove from the wrong directory (relative path issue)
  7. Using wildcards that don't match due to shell expansion

How to fix "fatal: pathspec 'file.txt' did not match any files"

1Check if the file is tracked by Git

First, verify whether Git knows about this file:

bash
git ls-files | grep filename

Or check the status:

bash
git status

If the file doesn't appear in the tracked files list or shows as "untracked," that's why git rm fails—there's nothing for Git to remove.

2If the file was already deleted with rm

If you already deleted the file using the regular rm command and now want to stage that deletion for commit, use:

bash
git add -u

This stages all modifications and deletions of already-tracked files. Alternatively, target the specific file:

bash
git add path/to/deleted-file.txt

Yes, you use git add to stage a deletion—Git tracks the change, not the existence.

3If the file was never tracked

For files that were never added to Git, just use the regular rm command:

bash
rm path/to/file.txt

Or to remove a directory:

bash
rm -rf path/to/directory/

Since Git doesn't track these files, there's no need to use git rm.

4Check for case sensitivity issues

On case-sensitive filesystems (Linux, macOS by default), File.txt and file.txt are different files. Verify the exact name:

bash
ls -la | grep -i filename

If there's a case mismatch, use the exact case as shown by ls:

bash
git rm "File.txt"  # Use exact case

On Windows, Git may track a file with different case than what the filesystem shows. Check what Git sees:

bash
git ls-files | grep -i filename
5Handle relative path issues

Make sure you're in the correct directory or using the right path. If you're not at the repository root:

bash
# From repo root
git rm src/components/file.txt

# Or use absolute path from repo root
git rm ./path/from/current/dir/file.txt

You can also navigate to the file's directory first:

bash
cd src/components
git rm file.txt
6Use --ignore-unmatch for scripts

If you're writing scripts or want to avoid errors when files might not exist, use the --ignore-unmatch flag:

bash
git rm --ignore-unmatch file.txt

This exits with zero status even if no files matched. Useful for CI/CD pipelines and automation scripts.

7Remove directories recursively

To remove a directory and all its contents, you need the -r flag:

bash
git rm -r path/to/directory/

Without -r, Git will not recursively remove directory contents and may fail with this error if you specify just the directory name.

Advanced notes

### git rm --cached: Remove from Index Only

If you want to stop tracking a file but keep it on your local filesystem, use:

bash
git rm --cached file.txt

This removes the file from Git's index (so it won't be tracked in future commits) but leaves it in your working directory. This is commonly used for:

- Removing accidentally committed .env files
- Untracking build artifacts that should have been in .gitignore
- Removing files from version control while keeping local copies

### Difference Between git rm and rm

| Command | Effect on Filesystem | Effect on Git Index |
|---------|---------------------|---------------------|
| rm file.txt | Deletes file | No change (must run git add) |
| git rm file.txt | Deletes file | Stages deletion for commit |
| git rm --cached file.txt | Keeps file | Stages deletion from tracking |

### Handling Files with Spaces or Special Characters

Quote filenames with spaces:

bash
git rm "my file.txt"
git rm 'path/to/my file.txt'

For files starting with a dash, use -- to separate options from arguments:

bash
git rm -- -filename-with-dash.txt

### Wildcards and Shell Expansion

When using wildcards, be aware of shell expansion:

bash
# Shell expands *.txt before git sees it
git rm *.txt

# To let git handle the pattern, quote it
git rm '*.txt'

The quoted version lets Git's internal matching handle the pattern, which works even for files in subdirectories.

### Removing Files from History

git rm only affects the current commit forward. To remove a file from all history (e.g., accidentally committed secrets), you need:

bash
git filter-branch --force --index-filter \
  'git rm -r --cached --ignore-unmatch path/to/file' \
  --prune-empty --tag-name-filter cat -- --all

Or use the newer BFG Repo-Cleaner tool for large repositories.

Frequently asked questions

What does "fatal: pathspec 'file.txt' did not match any files" mean?
When you run git rm, Git looks for the specified file in its index (the staging area). If the file is not being tracked by Git—meaning it was never added with git add or has already been removed from the index—Git cannot remove it and shows this error. Unlike the regular rm command which deletes files from your filesystem, git rm is specifically designed to remove tracked files from both your working directory and Git's index in a single operation.
How do I fix "fatal: pathspec 'file.txt' did not match any files"?
This error occurs when you try to use git rm on a file that Git is not tracking. The file may have already been deleted, never been added to Git, or you may have a typo in the filename.
What causes "fatal: pathspec 'file.txt' did not match any files"?
The file was already deleted from the filesystem using rm (not git rm) The file was never tracked by Git (never ran git add on it) Typo or misspelling in the filename
Is the "fatal: pathspec 'file.txt' did not match any files" error serious?
It is a low-severity error — more of a blocker than a risk. The steps above resolve it.

Official resources & further reading