Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

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

Git matches pathspecs against the index, not the disk. List tracked files, separate paths with --, quote globs, and use git switch for branches.

~5 min readGit
#git#pathspec#checkout#restore
Bold white headline reading NO PATHSPEC with a Git index crate unable to find an amber file card on the right.

Introduction & Quick Summary

A checkout, restore, or add refuses a path:

error: pathspec 'src/utils/helper.ts' did not match any file(s) known to git

or, for a branch:

error: pathspec 'feature/logn' did not match any file(s) known to git

The command received an argument it interprets as a path or a ref, and no tracked path in the current working tree matches it. Git says "known to git" precisely: the file may exist on disk and still be unknown if it is untracked, ignored, or excluded by a sparse checkout.

Where it appears:

  • git checkout -- file for a file that was never added, or was already deleted from the index.
  • A branch name typo, because git checkout <arg> tries a path first when the argument matches nothing in the ref namespace.
  • Running the command from a subdirectory while the path is written relative to the repository root.
  • git restore path on macOS or Windows where the filesystem is case-insensitive but the index stores the original case.
  • Shell expansion turning *.ts into a list that includes files Git does not track.

Quick answer: list what Git actually knows (git ls-files), separate paths from refs with --, quote the argument, and check the current directory before retrying.

Core Mechanics, Signatures & Reproducible Setup

A pathspec is a pattern matched against the index and the working tree, not against the filesystem alone. By default Git matches a leading directory and applies fnmatch semantics with FNM_PATHNAME, so *.ts does not cross directory boundaries and src/* matches one level. Pathspec magic prefixes change that: :(glob), :(icase), :(exclude), and :(top).

Reproduce the error in an empty repository:

bash
mkdir pathspec && cd pathspec && git init -q echo "console.log(1)" > app.js git status --short # ?? app.js -- untracked git checkout -- app.js # error: pathspec 'app.js' did not match any file(s) known to git git add app.js git commit -qm init git checkout -- app.js # now it works: the path is in the index

The same message appears for a branch that does not exist, because git checkout disambiguates in this order: a ref, then a path:

bash
git checkout feature/logn # error: pathspec 'feature/logn' did not match any file(s) known to git

Git only reports "did you mean this branch" when the fuzzy match is close enough, which is why a one-letter typo sometimes produces this error without a hint.

What each pathspec form matches:

FormMatchesNote
direverything under dirrecursive
*.ts*.ts in any directoryshell quoting matters
src/*.tsone level under srcFNM_PATHNAME
:(glob)**/*.tsany depthexplicit glob magic
:(exclude)docseverything except docscombine with --
:(top)app.jsroot-relative pathfrom a subdirectory
-- app.jsapp.js and nothing elseseparates paths from options

Key Comparison Table

Why a file that exists on disk is not "known to git":

Statels shows itgit ls-files shows itFix
untrackedyesnogit add <path>
ignoredyesnogit add -f <path> or edit .gitignore
staged then deleted from working treenoyesgit restore <path>
deleted from index as wellnonogit checkout HEAD -- <path>
sparse checkout excludednoyes, with skip-worktreegit sparse-checkout add <dir>
wrong case on a case-insensitive FSdependsonly the indexed casegit mv to rename

Commands whose first argument can be a path or a ref, and how to disambiguate:

CommandAmbiguitySafe form
git checkout xbranch or pathgit switch x or git checkout -- x
git restore xpath only in 2.23+git restore --source=HEAD -- x
git reset xcommit or pathgit reset -- x
git show xobject or pathgit show HEAD:x
git diff xcommit or pathgit diff -- x

Practical Edge Cases, Error Fixes & Deep Dive Code Examples

1. Finding the real path Git knows

Do not guess the string. Ask the index, case-insensitively:

bash
git ls-files | grep -i 'helper' git ls-files --error-unmatch src/utils/helper.ts

--error-unmatch exits non-zero with the same pathspec message when the path is not tracked, which makes it usable in scripts. If the file is new, the answer is git add; if it is ignored, git check-ignore -v src/utils/helper.ts names the .gitignore rule responsible.

2. Branch typos and the path-first disambiguation

git checkout treats an unknown ref as a path. Use git switch for branches, which never does:

bash
git switch feature/login # clear error: "invalid reference" git branch --list 'feature/*' # what actually exists

In scripts, prefer git switch over git checkout for ref changes and git restore for file changes. Both were added in Git 2.23 exactly to remove this ambiguity, and both give messages that name the missing ref instead of the missing path.

3. Case-insensitive filesystems

On macOS and Windows the filesystem folds case, while the index is case-sensitive. A file committed as Button.tsx, imported as button.tsx, and referenced as git add button.tsx can fail with this message on one machine and succeed on a Linux CI runner. Detect and fix the stored case:

bash
git config core.ignorecase # true on macOS/Windows git ls-files | grep -i button git mv --force Button.tsx Button.tsx # no-op rename refreshes the index

Avoid git config core.ignorecase false on a case-insensitive disk: it makes Git report every renamed-only-by-case file as deleted, and a checkout can then remove the working copy.

4. Running from the wrong directory

From packages/web, the path src/index.ts refers to packages/web/src/index.ts. Git matches pathspecs relative to the current directory, which is different from the repository root used in most documentation:

bash
git rev-parse --show-toplevel # repository root git ls-files --full-name src/index.ts # path as stored git restore -- :(top)packages/web/src/index.ts

The :(top) magic prefix is the reliable way to write root-relative paths in shared scripts.

5. Shell expansion and special characters

An unquoted pattern is expanded by the shell before Git sees it. When the expansion produces a file Git does not track, the whole command fails:

bash
git add src/*.ts # shell expands; one ignored file aborts the add git add 'src/*.ts' # quoted: Git expands, ignored paths skipped git add -- src/**/*.ts # bash needs globstar for this form

Filenames containing a leading dash need -- as well: git add -- -weird-name.txt. Without it, Git reads the name as an option and reports a pathspec error.

Common Compilation/Runtime Mistakes & Version Gotchas

Retrying with -f/--force. Force flags do not make an untracked or ignored path match; git add -f only bypasses .gitignore, and it fails with the same pathspec message when the file does not exist at all.

Assuming git checkout . clears everything. It restores tracked files under the current directory only. Untracked files stay, and from a subdirectory the rest of the tree is untouched. Use git restore --source=HEAD --worktree --staged :/ deliberately when that is what you mean.

Deleting with rm and expecting Git to follow. Removing a file with rm leaves the index entry until git add -A or git rm records it; after that, git checkout -- file reports a pathspec error because the path is gone from both index and tree. Restore from HEAD instead.

Version differences worth knowing. git restore and git switch exist from Git 2.23; older distributions still ship 2.20–2.22, where the error messages and flags differ. Git 2.25 changed the default of fetch.negotiationAlgorithm, unrelated to pathspecs but often upgraded at the same time and blamed for the same failures. Git 2.37 and later make git ls-files --deduplicate useful on case-insensitive checkouts for finding entries that differ only by case, which is the fastest way to detect a rename that will break Linux builds.

Checking what a pattern would match. Run the pattern through the regular expression tester to see whether it behaves like the glob you intended, and compare two tree listings with Diff when the same path resolves on one machine and not another.

Reference material: Git glossary, pathspec and git-add.

Official references

primary documentation

The Git documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit Git work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Git guides

More in Git

All Git guides

The guides filed next to this one in Git, in library order.