How to fix pathspec did not match any file(s) known to git in Git

GitBEGINNERLOWUpdated May 28, 2026· 600 views

Fix Git's "pathspec did not match any file(s)" error caused by typos, untracked files, or unfetched branches. Step-by-step checks and fixes.

Quick fix

Fixes pathspec did not match any file(s) known to git

# List all local branches
git branch

# List all branches including remote tracking branches
git branch -a

# Search for a specific branch pattern
git branch -a | grep -i feature
  1. Verify the file or branch actually exists
  2. Fetch remote branches before checking out
  3. Check for case sensitivity issues
  4. Handle files in .gitignore or untracked files
git — error
# List all local branches
git branch
# List all branches including remote tracking branches
git branch -a
# Search for a specific branch pattern
git branch -a | grep -i feature
pathspec did not match any file(s) known to git

What this error means

The "pathspec did not match any file(s) known to git" error means Git cannot locate the file, directory, or branch reference you specified in your command. A "pathspec" in Git terminology is the pattern or path argument you provide to commands like `git checkout`, `git add`, `git rm`, or `git diff`. This error appears because Git only operates on files and references it knows about. If a file isn't tracked (hasn't been added to the repository), if a branch hasn't been fetched from the remote, or if there's simply a typo in your command, Git will report this error. The error is intentionally vague about whether you're referring to a file or a branch. When you run `git checkout foo`, Git first checks if "foo" is a branch name. If not, it looks for a file or directory named "foo". When neither exists, you get this error. Common scenarios that trigger this error: - Trying to checkout a remote branch that hasn't been fetched locally - Mistyping a filename or branch name (including case sensitivity issues) - Attempting to add or commit a file that doesn't exist or is in .gitignore - Working in the wrong repository or directory - Referencing a file that was renamed or deleted

Typical symptoms

  • Git checkout command fails when trying to switch to a branch
  • Git add command reports the file doesn't match when you can see it exists
  • Git rm fails even though the file appears to exist on disk
  • Git stash apply or pop fails with pathspec error for specific files
  • CI/CD pipeline fails at a git checkout step
  • Tab completion in terminal works but the git command still fails
  • Command works for team members but not on your machine

Common causes

  1. The file or directory path has a typo or is misspelled
  2. The branch exists on the remote but hasn't been fetched locally
  3. Case sensitivity mismatch (especially on Linux or when transferring between OS)
  4. The file exists but isn't tracked by Git (not added or in .gitignore)
  5. You're in the wrong repository or working directory
  6. Git submodules haven't been initialized
  7. The file was renamed or deleted in the branch you're checking out
  8. Special characters or spaces in the filename aren't properly quoted

How to fix "pathspec did not match any file(s) known to git"

1Verify the file or branch actually exists

First, confirm that what you're looking for actually exists:

For branches:

bash
# List all local branches
git branch

# List all branches including remote tracking branches
git branch -a

# Search for a specific branch pattern
git branch -a | grep -i feature

For files:

bash
# List files Git is tracking
git ls-files | grep -i filename

# Check if file exists on disk
ls -la path/to/file

# Search for files matching a pattern
find . -name "*filename*" -type f

Compare what you typed against the actual name. Pay attention to:
- Exact spelling
- Case (uppercase vs lowercase)
- Hyphens vs underscores
- File extensions

2Fetch remote branches before checking out

If you're trying to checkout a branch that exists on GitHub/GitLab but not locally, you need to fetch it first:

bash
# Fetch all branches from all remotes
git fetch --all

# Fetch from a specific remote
git fetch origin

# Now try checking out the branch
git checkout feature-branch

# Or explicitly track the remote branch
git checkout -b feature-branch origin/feature-branch

Alternative with git switch (Git 2.23+):

bash
git switch feature-branch

# If the branch doesn't exist locally, create it tracking remote
git switch -c feature-branch origin/feature-branch

After fetching, git branch -a will show remote branches like remotes/origin/feature-branch.

3Check for case sensitivity issues

Git is case-sensitive, but your filesystem might not be. This causes confusion especially when:
- Moving code between Windows/Mac (case-insensitive) and Linux (case-sensitive)
- Renaming files with only case changes

Check the actual case in Git's index:

bash
# See exactly how Git stores the filename
git ls-files | grep -i yourfile

# Check the case in the branch
git ls-tree -r HEAD --name-only | grep -i yourfile

Fix case sensitivity issues:

bash
# Option 1: Use the exact case Git knows about
git checkout -- "src/Components/Header.jsx"  # Not "header.jsx"

# Option 2: Rename with two steps (to avoid case-insensitive filesystem issues)
git mv OldName.txt temp.txt
git mv temp.txt newname.txt

Verify your file system's case sensitivity:

bash
# On Mac, check if the volume is case-sensitive
diskutil info / | grep "Case-sensitive"
4Handle files in .gitignore or untracked files

If a file exists on disk but Git can't find it, it might be ignored or not yet tracked:

bash
# Check if the file is being ignored
git check-ignore -v path/to/file

# List all ignored files
git status --ignored

# See what .gitignore rule is matching
git check-ignore -v --no-index path/to/file

If you need to track an ignored file:

bash
# Force add an ignored file
git add -f path/to/file

# Or remove the rule from .gitignore first
# Then add normally
git add path/to/file

If the file simply hasn't been added yet:

bash
# Add the file to start tracking it
git add path/to/newfile.txt

# Then your checkout/stash commands will work
5Verify you're in the correct repository

A surprisingly common cause is being in the wrong directory:

bash
# Check which repository you're in
git remote -v

# See the repository root
git rev-parse --show-toplevel

# Confirm the current working directory
pwd

If you have multiple clones of similar repositories (dev, staging, production), you might be in the wrong one:

bash
# Navigate to the correct repository
cd /path/to/correct/repo

# Then retry your command
git checkout feature-branch

Also verify you're not accidentally in a subdirectory when the file is in the root:

bash
# Go to repo root
cd $(git rev-parse --show-toplevel)

# Now try your command with full path
git add path/from/root/file.txt
6Initialize Git submodules if needed

If the pathspec error refers to files inside a submodule directory, the submodules may not be initialized:

bash
# Initialize all submodules
git submodule update --init

# Initialize and update recursively
git submodule update --init --recursive

# Check submodule status
git submodule status

After initializing submodules:

bash
# The submodule directories should now have content
ls path/to/submodule/

# And Git operations on those files should work
git diff path/to/submodule/file.txt

If you cloned a repository with --depth (shallow clone), submodules might be incomplete. Try:

bash
git fetch --unshallow
git submodule update --init --recursive
7Handle special characters and spaces in paths

Filenames with spaces, quotes, or special characters need proper escaping:

bash
# Use quotes for paths with spaces
git add "path/to/my file.txt"
git checkout -- "My Documents/config.json"

# Escape special characters
git add path/to/file\(1\).txt

# Use single quotes to preserve literal characters
git checkout -- 'file with $pecial chars.txt'

For wildcards and glob patterns:

bash
# Escape asterisks in filenames
git add 'test*.txt'  # Matches files starting with "test"

# To match a literal asterisk in the name
git add 'test\*.txt'

# Use -- to separate options from paths
git checkout -- *.txt

Dealing with unusual path separators:

bash
# Always use forward slashes, even on Windows
git add src/components/Header.tsx  # Correct
git add src\components\Header.tsx  # May cause issues
8Prune stale remote-tracking branches

If a branch was deleted on the remote but you still see it locally, you might try to checkout a branch that no longer exists:

bash
# See what would be pruned
git remote prune origin --dry-run

# Remove stale remote-tracking branches
git remote prune origin

# Or fetch with prune in one command
git fetch --prune

# Set prune to happen automatically on fetch
git config --global fetch.prune true

After pruning, git branch -a will only show branches that actually exist on the remote.

If you had a local branch tracking a now-deleted remote:

bash
# Delete the local branch
git branch -d old-feature-branch

# Or force delete if it has unmerged changes
git branch -D old-feature-branch

Advanced notes

Understanding Pathspecs in Git:

A pathspec is a pattern that Git uses to limit operations to specific paths. It can be:
- A literal path: src/index.js
- A directory: src/ (note the trailing slash)
- A glob pattern: *.txt, src/**/*.js
- A magic signature: :(top), :(exclude)pattern

Pathspec Magic Signatures:

bash
# Start from repo root regardless of current directory
git add ':(top)path/from/root/file.txt'

# Exclude patterns
git diff -- . ':(exclude)*.log'

# Case-insensitive matching
git add ':(icase)readme.md'

Debugging Pathspec Issues:

Use GIT_TRACE to see exactly what Git is doing:

bash
GIT_TRACE=1 git checkout feature-branch 2>&1 | head -50

Disambiguation Between Branches and Files:

When both a branch and a file could match, use -- to clarify:

bash
# Explicitly specify you mean a file, not a branch
git checkout -- filename

# Explicitly specify you mean a branch
git checkout branch-name --

Sparse Checkouts:

If you're using sparse-checkout, files outside the sparse patterns won't be checked out:

bash
# Check your sparse-checkout configuration
git sparse-checkout list

# Add a path to sparse-checkout
git sparse-checkout add path/to/directory

The "Detached HEAD and Pathspec" Edge Case:

When in a detached HEAD state, some operations behave differently:

bash
# Check if you're detached
git symbolic-ref HEAD 2>/dev/null || echo "Detached HEAD"

# Reattach to a branch before checking out files
git checkout main
git checkout -- path/to/file

Repository Corruption:

In rare cases, the index or object database can become corrupted:

bash
# Check repository integrity
git fsck --full

# If issues found, try rebuilding the index
rm .git/index
git reset

# Or restore from a fresh clone as last resort

Large Repositories and Partial Clones:

With partial clones (--filter=blob:none), files might not be fetched:

bash
# Fetch a specific file in a partial clone
git fetch --filter=blob:none origin
git checkout origin/main -- path/to/file

# Or convert to full clone
git fetch --unshallow

Frequently asked questions

What does "pathspec did not match any file(s) known to git" mean?
The "pathspec did not match any file(s) known to git" error means Git cannot locate the file, directory, or branch reference you specified in your command. A "pathspec" in Git terminology is the pattern or path argument you provide to commands like git checkout, git add, git rm, or git diff. This error appears because Git only operates on files and references it knows about.
How do I fix "pathspec did not match any file(s) known to git"?
Fix Git's "pathspec did not match any file(s)" error caused by typos, untracked files, or unfetched branches. Step-by-step checks and fixes.
What causes "pathspec did not match any file(s) known to git"?
The file or directory path has a typo or is misspelled The branch exists on the remote but hasn't been fetched locally Case sensitivity mismatch (especially on Linux or when transferring between OS)
Is the "pathspec did not match any file(s) known to git" error serious?
It is a low-severity error — more of a blocker than a risk. The steps above resolve it.

Official resources & further reading