Git

How to Fix "fatal: pathspec did not match any file(s) known to git"

4 min read by DebuggedIt

Quick answer

You run a Git command targeting a file or branch and get a "pathspec" error instead of the expected result. This means Git looked for something matching the...

You run a Git command targeting a file or branch and get a "pathspec" error instead of the expected result. This means Git looked for something matching the name you gave it β€” a file, a branch, a tag β€” and found nothing that matches in the context you're using.

The Problem

The most common trigger is checking out a branch that doesn't exist, or trying to operate on a file Git doesn't know about:

$ git checkout feature/new-ui
error: pathspec 'feature/new-ui' did not match any file(s) known to git

It also shows up with git add or git checkout on individual files:

$ git checkout -- config.yml
error: pathspec 'config.yml' did not match any file(s) known to git

Or with git rm:

$ git rm old-script.py
fatal: pathspec 'old-script.py' did not match any files

Why It Happens

Git's pathspec resolver checks the name you passed against the working tree, the index, or known refs, depending on the command. The error fires for one of these reasons:

  • Typo or wrong path β€” the filename or branch name is misspelled, or you're not in the directory you think you're in.
  • Branch doesn't exist locally β€” you're trying to git checkout a remote branch that hasn't been fetched yet, so it isn't in your local branch list.
  • File isn't tracked β€” for git checkout -- file or git rm, the file has to already be tracked by Git (added and committed, or at least staged) β€” untracked files aren't valid pathspecs for these commands.
  • Case sensitivity mismatch β€” on case-insensitive filesystems (default on macOS and Windows), a file named Config.yml vs config.yml can behave inconsistently between your OS and Git's internal case-sensitive tracking.
  • .gitignore is excluding the file β€” if the file matches a pattern in .gitignore, some commands treat it as if it doesn't exist for pathspec purposes.

The Fix

Start by confirming what Git actually sees. List local branches:

git branch -a

If the branch you want only appears under remotes/origin/..., fetch it and check it out with tracking:

git fetch origin
git checkout -b feature/new-ui origin/feature/new-ui

For file-related errors, check whether the file is tracked:

git status
git ls-files | grep config.yml

If it shows up under "Untracked files" in git status, that's your answer β€” git checkout -- file and git rm only work on tracked files. To remove an untracked file, use the filesystem directly or:

rm config.yml

If it's a case-sensitivity issue, check the exact filename Git has recorded:

git ls-files | grep -i config

and use that exact casing in your command. Also double check your current directory and spelling:

pwd
ls -la

Still Not Working?

If the file clearly exists, is tracked, and the name is correct, but the error persists, you might be running the command from a subdirectory with a path that needs to be relative to your current location rather than the repo root. Try running from the repository root with an explicit relative path:

cd $(git rev-parse --show-toplevel)
git checkout -- path/to/config.yml

If it's still failing on a branch name, confirm you don't have a stray typo character (curly quotes copied from a chat app or doc are a surprisingly common culprit) by retyping the command manually instead of pasting it.

Another common trap is running a Git command intended for a specific file, but accidentally passing a directory name that doesn't exist yet, or a glob pattern your shell already expanded before Git ever saw it. If you're using a wildcard like *.log and get the pathspec error, check whether your shell silently passed the literal string because no files matched:

$ git rm *.log
fatal: pathspec '*.log' did not match any files

Run ls *.log first to confirm the shell can actually see matching files in the current directory before blaming Git for the mismatch.

It's also worth remembering that some Git commands distinguish between a branch name and a file path using a double-dash separator (--). If you have a file and a branch with the same name, Git can get confused about which one you mean:

git checkout -- config.yml

The -- here explicitly tells Git "everything after this is a pathspec, not a branch or ref," which resolves ambiguity in cases where a name could plausibly refer to either. Leaving it out in ambiguous situations is a subtle but common source of this exact error.