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.

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 -- filefor 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 pathon macOS or Windows where the filesystem is case-insensitive but the index stores the original case.- Shell expansion turning
*.tsinto 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:
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 indexThe same message appears for a branch that does not exist, because git checkout disambiguates in this order: a ref, then a path:
git checkout feature/logn
# error: pathspec 'feature/logn' did not match any file(s) known to gitGit 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:
| Form | Matches | Note |
|---|---|---|
dir | everything under dir | recursive |
*.ts | *.ts in any directory | shell quoting matters |
src/*.ts | one level under src | FNM_PATHNAME |
:(glob)**/*.ts | any depth | explicit glob magic |
:(exclude)docs | everything except docs | combine with -- |
:(top)app.js | root-relative path | from a subdirectory |
-- app.js | app.js and nothing else | separates paths from options |
Key Comparison Table
Why a file that exists on disk is not "known to git":
| State | ls shows it | git ls-files shows it | Fix |
|---|---|---|---|
| untracked | yes | no | git add <path> |
| ignored | yes | no | git add -f <path> or edit .gitignore |
| staged then deleted from working tree | no | yes | git restore <path> |
| deleted from index as well | no | no | git checkout HEAD -- <path> |
| sparse checkout excluded | no | yes, with skip-worktree | git sparse-checkout add <dir> |
| wrong case on a case-insensitive FS | depends | only the indexed case | git mv to rename |
Commands whose first argument can be a path or a ref, and how to disambiguate:
| Command | Ambiguity | Safe form |
|---|---|---|
git checkout x | branch or path | git switch x or git checkout -- x |
git restore x | path only in 2.23+ | git restore --source=HEAD -- x |
git reset x | commit or path | git reset -- x |
git show x | object or path | git show HEAD:x |
git diff x | commit or path | git 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:
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:
git switch feature/login # clear error: "invalid reference"
git branch --list 'feature/*' # what actually existsIn 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:
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 indexAvoid 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:
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.tsThe :(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:
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 formFilenames 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 documentationThe Git documentation that defines the behaviour this guide explains.
Try matching browser tools
All toolsSmall utilities that fit Git work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Git guides
All Git guidesGit: fatal: not a git repository (or any parent): .git
The command ran outside a working tree or the .git metadata is gone. Confirm the root, restore a deleted .git, and repair worktree pointers.
Git: fatal: Need to specify how to reconcile divergent branches
Git 2.34 and later refuse to guess between merge, rebase and fast-forward. Set pull.rebase or pull.ff once, and know what each strategy does to history.
Git: fatal: unable to auto-detect email address
A commit needs an author identity. Containers and fresh installs have no user.email, so Git guesses from the OS user and refuses to continue.
Git: could not read Username, terminal prompts disabled
A headless git has no terminal to ask for credentials. Supply a token through a helper, a header or SSH instead of relying on a prompt.
More in Git
All Git guidesThe guides filed next to this one in Git, in library order.