日本語

How to Fix the Git “pathspec did not match” Error

​​Git​command line,Troubleshooting

error: pathspec '…' did not match any file(s) known to git means Git could not find any file matching the name you passed. You can see it even if you did not mean to specify a file. When you try to switch branches with git checkout and the name is not found as a branch, Git treats the name as a file path instead.

If you get this error when switching to a branch that should exist on the remote, or when using git add or git rm with a file, check three things: a typo in the name, whether you forgot to run git fetch, and which directory you ran the command from. Below, I reproduce the error for each cause and show the actual output, both for the error and after the fix.

Note

I tested on Ubuntu 24.04 with Git 2.43.0 and bash 5.2. I ran the commands with LANG=C so the messages would be in English. The remote was a local bare repository created in the same environment and registered with the relative path ../remote.git. I did not test with hosting services such as GitHub, or on Windows or macOS, where file names are case-insensitive. I fixed the commit dates, so the hashes will differ if you run these commands yourself.

A pathspec is a file specifier

A pathspec is a specifier for files that you pass to a Git command. The pathspec entry in gitglossary describes it as "a pattern used to limit paths in Git commands." It is used with commands such as git add, git checkout, and git ls-files. You can use wildcards like *.log, not just file names.

Depending on the command, I got this error in two forms.

Command Message Exit code
git checkout, git restore, git commit error: pathspec 'name' did not match any file(s) known to git 1
git add, git rm fatal: pathspec 'name' did not match any files 128

Both mean that no file matched the name you passed. The error appears when you only wanted to switch branches because of how git checkout works. The git checkout reference covers both switching branches and restoring files. In my test, when I passed a name that was not found as a branch, Git searched for it as a file path. When no file matched either, I got this error.

Flowchart of what git checkout does with a name. If a local branch with that name exists, it switches to it. If not, and exactly one remote-tracking branch with that name exists, it creates a branch and switches to it. If neither exists, it treats the name as a file path. If a tracked file matches, it restores that file. If none matches, it stops with the pathspec did not match error.
git checkout treats a name that is not a branch as a file path

When I passed the name of a tracked file, its changes were discarded and the file was restored.

console
$ git checkout README.md
Updated 1 path from the index

I mistyped the branch name

When I passed a branch name that did not exist to git checkout, I got an error.

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

When I added -- after the name, the message changed to one meaning that no reference (such as a branch) had that name. As described in the Git command-line conventions (gitcli), -- separates revisions such as branches from file paths, so the name before it is not treated as a file. git switch only switches branches, so it failed with this message from the start.

console
$ git checkout feature/logn --
fatal: invalid reference: feature/logn

To check the correct name, run git branch -a. The -a option lists both local branches and the remote branches recorded locally (remotes/origin/…).

console
$ git branch -a
* main
  remotes/origin/HEAD -> origin/main
  remotes/origin/main

If the branch you want is in this list, copy the name and retype it to switch. If it is not in the list, check the next section, which covers a branch you have not fetched yet.

The branch exists on the remote, but I have not fetched it

I had pushed the branch feature/login from another working directory, so it existed on the remote. I had not run git fetch locally yet. When I tried to switch to it, git checkout gave the pathspec error and git switch gave the invalid reference error.

console
$ git checkout feature/login
error: pathspec 'feature/login' did not match any file(s) known to git

$ git switch feature/login
fatal: invalid reference: feature/login

$ git branch -r
  origin/HEAD -> origin/main
  origin/main

origin/feature/login is missing from the output of git branch -r. This is because the local record of the remote is out of date. After I ran git fetch to bring it in, the branch appeared in the list, and the same git checkout feature/login succeeded.

console
$ git fetch
From ../remote
 * [new branch]      feature/login -> origin/feature/login

$ git branch -r
  origin/HEAD -> origin/main
  origin/feature/login
  origin/main

$ git checkout feature/login
Switched to a new branch 'feature/login'
branch 'feature/login' set up to track 'origin/feature/login'.

$ git branch -vv
* feature/login e0777b5 [origin/feature/login] ログイン画面
  main          b905279 [origin/main] 最初のコミット

I did not have a local feature/login branch yet, but Git created one from origin/feature/login and switched to it. The git checkout reference says that when a remote-tracking branch with the same name exists on exactly one remote, the command behaves like git checkout -b feature/login --track origin/feature/login. The description of --guess in git switch says the same, so git switch feature/login also works.

I did not quote the commit message

If you write a commit message containing spaces in git commit -m without quotes, everything after the first space is treated as a pathspec. When I wrote README に使い方を追加, only README was taken as the message, and Git looked for a file named に使い方を追加 and failed. No commit was created.

console
$ git commit -m README に使い方を追加
error: pathspec 'に使い方を追加' did not match any file(s) known to git

$ git log --oneline -1
b905279 最初のコミット

According to the git commit reference, -m takes only the single word right after it as the message, and the names that follow are pathspecs (the files to include in the commit). When I put the whole message in quotes, the commit succeeded.

console
$ git commit -m "README に使い方を追加"
[main b50b373] README に使い方を追加
 1 file changed, 1 insertion(+)

When I used -a, as in git commit -am fix typo, the command stopped with the following message instead of the pathspec error. Quoting the message fixes this too.

console
$ git commit -am fix typo
fatal: paths 'typo ...' with -a does not make sense

The file name differs in case

On the test environment (Linux), file names are case-sensitive. In a repository that only had README.md, git add readme.md failed.

console
$ git add readme.md
fatal: pathspec 'readme.md' did not match any files

$ git ls-files
README.md
src/app.js

Running git ls-files shows the correct names of the files Git tracks. For a newly created file, check with ls or git status. Running git add README.md, using the exact name shown, succeeded with no error.

I used a repository-root path from a subdirectory

Paths passed to git add and similar commands are interpreted relative to the current directory. After I moved into the src directory, git add src/app.js looked for src/src/app.js and failed.

console
$ git add src/app.js
warning: could not open directory 'src/src/': No such file or directory
fatal: pathspec 'src/app.js' did not match any files

Either change the path to be relative to the current directory, or prefix it with :/. According to the pathspec entry in gitglossary, a pathspec starting with :/ is interpreted from the repository root even when you run the command in a subdirectory. Both approaches worked. For the second, I first undid the first addition with git reset.

console
$ git add app.js
$ git status --short
M  app.js

$ git reset -q
$ git status --short
 M app.js

$ git add :/src/app.js
$ git status --short
M  app.js

git status --short also shows paths relative to the current directory, so it displays app.js rather than src/app.js.

I used git rm on a file that is not tracked

git rm removes files that Git tracks. When I specified notes.txt, which I had created but never added with git add, I got an error with or without --cached.

console
$ git rm notes.txt
fatal: pathspec 'notes.txt' did not match any files

$ git rm --cached notes.txt
fatal: pathspec 'notes.txt' did not match any files

This file is not under Git's control, so there is nothing for git rm to remove. If you want to delete the file itself, remove it with rm or a similar command. To check whether a file is tracked, use git ls-files --error-unmatch. For an untracked file, it gave the same kind of error plus a hint about whether you forgot git add.

console
$ git ls-files --error-unmatch notes.txt
error: pathspec 'notes.txt' did not match any file(s) known to git
Did you forget to 'git add'?

In a shell script, if you want to continue even when the file may not exist, add --ignore-unmatch. As described in the git rm reference, the command exited with status 0 and printed nothing when no file matched.

console
$ git rm --ignore-unmatch notes.txt

I tried to restore a file I never committed with git restore

According to the git restore reference, without --staged, git restore returns a file to the content recorded by git add (the index). A file that was created but never added with git add is not in the index, so the command failed.

console
$ git restore memo.txt
error: pathspec 'memo.txt' did not match any file(s) known to git

$ git status --short
?? memo.txt

?? means Git is not tracking the file. I cover how to use git restore in more detail in a separate article on restoring deleted files and changes with git restore.

No file matches the wildcard

When I passed a wildcard such as git add '*.log' and no file matched it, I also got an error.

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

As noted in the pathspec entry in gitglossary, a * passed to Git also matches files in subdirectories. So to check whether any files match, use git ls-files rather than ls. In the repository root, ls *.js found nothing, but git ls-files '*.js' listed src/app.js. For '*.log', nothing was listed, which shows that no file matches.

console
$ ls *.js
ls: cannot access '*.js': No such file or directory

$ git ls-files '*.js'
src/app.js

$ git ls-files '*.log'

When I passed '*.js', which matches a file, to git add, it finished with exit status 0 and no error. git ls-files only lists files Git tracks, so check newly created files with git status.

Point

If you see pathspec did not match, first decide whether you were trying to switch branches or specify a file, since that determines where to look. For a branch, check the name with git branch -a, and if it is not listed, run git fetch. For a file, confirm the exact name with git ls-files or git status, and check that the path is relative to your current directory.

​Posted by 86