5.

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

編集
この記事の要点
  • error: pathspec '...' did not match any file(s) known to git指定したパスが git の管理下にない
  • もっとも多い原因: git checkout branch_name でブランチ名のタイポ / リモート未取得
  • 対処 ①: git fetch でリモートブランチ情報を更新
  • 対処 ②: git branch -a でブランチ一覧確認
  • ファイル名なら git status でステージ可能なファイル確認

 

エラーの状況

# ブランチ切替で
$ git checkout feature/login
error: pathspec 'feature/login' did not match any file(s) known to git

# ファイル add で
$ git add my-file.txt
fatal: pathspec 'my-file.txt' did not match any files

# ファイル checkout で
$ git checkout HEAD~1 path/to/file
error: pathspec 'path/to/file' did not match any file(s) known to git

パターン別の原因と対処

パターン 1: ブランチ名のタイポ / リモートのみ存在

# ブランチ一覧確認
$ git branch -a
  develop
  main
* feature/login
  remotes/origin/HEAD -> origin/main
  remotes/origin/develop
  remotes/origin/feature/auth     ← リモートのみ
  remotes/origin/feature/login

# ローカルにないがリモートにある場合
$ git fetch origin
$ git checkout feature/auth
# → 自動的に remote から tracking branch 作成

パターン 2: 新規ブランチ作成 (-b 忘れ)

# ダメな例
$ git checkout new-branch
error: pathspec 'new-branch' did not match...

# 修正: 新規ブランチなら -b
$ git checkout -b new-branch
Switched to a new branch 'new-branch'

# Git 2.23+ の新コマンド
$ git switch -c new-branch

パターン 3: ファイルパスの誤り

# 大文字小文字違い
$ git add MyFile.txt
fatal: pathspec 'MyFile.txt' did not match any files

$ ls
myfile.txt    ← 小文字

$ git add myfile.txt   # 修正

# .gitignore に含まれている
$ cat .gitignore
*.log
build/

$ git add build/output.log
fatal: pathspec...
# → .gitignore に書かれているので追跡対象外
$ git add -f build/output.log  # 強制追加 (非推奨)

パターン 4: 未取得のリモートブランチ

# 同僚が push したばかりのブランチ
$ git checkout feature/recently-pushed
error: pathspec...

# リモートを更新してから
$ git fetch origin
$ git checkout feature/recently-pushed
# または
$ git checkout -b feature/recently-pushed origin/feature/recently-pushed

パターン 5: タグやコミット ID

# 古いタグ名のタイポ
$ git checkout v1.0
error: pathspec 'v1.0' did not match...

$ git tag -l
v1.0.0
v1.1.0
v2.0.0

$ git checkout v1.0.0  # 正しいタグ名

パターン 6: detached HEAD 後の操作

# 特定のコミットに移動した状態
$ git checkout abc1234
Note: switching to 'abc1234'.
You are in 'detached HEAD' state.

# そこから新ブランチを切らずにファイル変更
$ git add new-file
fatal: pathspec 'new-file'...

# 解決
$ git switch -c temp-branch
# → これでブランチが切られて add 可能

パターン 7: スパースチェックアウト

# sparse-checkout 有効化中
$ git sparse-checkout init
$ git sparse-checkout set src/

# 範囲外のファイルを操作しようとする
$ git checkout HEAD docs/README.md
error: pathspec 'docs/README.md' did not match...

# 対処: sparse 設定を更新
$ git sparse-checkout add docs/
# または無効化
$ git sparse-checkout disable

調査コマンド

# 管理されているファイル一覧 (HEAD basis)
$ git ls-files
src/main.py
src/util.py
README.md

# 特定の名前で検索 (部分一致)
$ git ls-files | grep -i myfile

# git 管理下にない (untracked) ファイル
$ git status

# ブランチとタグ
$ git branch -a
$ git tag -l

# リモートブランチを最新化
$ git fetch --all --prune

# 履歴を遡って特定ファイルが存在したコミット
$ git log --all --full-history -- "path/to/file"

# 削除されたファイルを探す
$ git log --diff-filter=D --summary | grep delete

未追跡ファイルを git に追加

$ git status
On branch main
Untracked files:
  (use "git add ..." to include in what will be committed)
        new-file.py

$ git add new-file.py
# OK

削除済みファイルを復元

# どのコミットで削除されたか
$ git log --diff-filter=D --summary | grep -A1 "delete mode"
commit abc1234
    delete mode 100644 deleted-file.py

# 削除前のバージョンを復元
$ git checkout abc1234^ -- deleted-file.py
$ git add deleted-file.py
$ git commit -m "restore deleted-file.py"

類似エラー

エラー意味
pathspec did not match any filesこのページ
did not find any matching refブランチ / タグ参照失敗
fatal: not a git repositorygit 管理されていないディレクトリ
You have unstaged changes未コミット変更で operation 失敗
refusing to merge unrelated histories共通祖先がない (--allow-unrelated-histories)

関連記事

編集
Post Share
子ページ

子ページはありません

同階層のページ
  1. fatal: remote origin already exists.
  2. fatal: '~' does not appear to be a git repository
  3. Cannot rebase: You have unstaged changes. Please commit or stash them.
  4. remote: error: denying non-fast-forward refs/heads/master (you should pull first)
  5. error: pathspec ... did not match any file(s) known to git.
  6. The following untracked working tree files would be overwritten by checkout
  7. fatal: Not a valid object name: 'master'.
  8. Unlink of file 'ファイル名' failed. Should I try again? (y/n)
  9. Another git process seems to be running in this repository, ~
  10. error: Your local changes to the following files would be overwritten by checkout: