タイトル: fatal: '~' does not appear to be a git repository
SEOタイトル: git「fatal: '...' does not appear to be a git repository」の原因と対処
| この記事の要点 |
|
このエラーの概要
git コマンドを実行したときに表示される、最も基本的なエラーの一つです:
# パターン1: カレントが git 管理下じゃない
$ git status
fatal: not a git repository (or any of the parent directories): .git
# パターン2: リモート操作
$ git push origin main
fatal: 'origin' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
# パターン3: クローン時
$ git clone git@github.com:user/typo-repo.git
Cloning into 'typo-repo'...
ERROR: Repository not found.
fatal: Could not read from remote repository.
# パターン4: SSH 認証失敗
$ git pull
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.
つまり 「ここは git リポジトリじゃないか、リモート先のリポジトリが見つからない」 と git が訴えています。原因は複数あるので順番に切り分けます。
原因の切り分けフロー
| シナリオ | 切り分けコマンド | 対処 |
|---|---|---|
| ローカルが git 管理下でない | ls -la .git | git init(対処1) |
| origin 未設定 | git remote -v | git remote add origin URL(対処2) |
| URL タイポ | git remote get-url origin | git remote set-url(対処3) |
| SSH 鍵未登録 | ssh -T git@github.com | 鍵登録(対処4) |
| 権限不足 | ブラウザで該当 URL を開く | 招待 / トークン更新(対処5) |
| VPN/プロキシ | ping git.internal / curl -I URL | VPN 接続(対処6) |
対処1: ローカルディレクトリが git 管理下でない
# まず現在地と .git の有無を確認
pwd
ls -la | grep .git
# 親ディレクトリにも .git が無いか
git rev-parse --show-toplevel
# fatal: not a git repository (or any of the parent directories)
# → どこにも無い
# 新規 git リポジトリにする場合
git init
git add .
git commit -m "Initial commit"
# 既存リポジトリをクローンしたい場合
cd ..
git clone https://github.com/user/repo.git
cd repo
対処2: origin が未設定 / 別名で登録されている
# リモート一覧
git remote -v
# (何も表示されない → origin 未設定)
# origin 追加
git remote add origin https://github.com/user/repo.git
# 確認
git remote -v
# origin https://github.com/user/repo.git (fetch)
# origin https://github.com/user/repo.git (push)
# 初回 push(上流ブランチ設定込み)
git push -u origin main
対処3: リモート URL のタイポ修正
# 現在の URL
git remote get-url origin
# URL を書き換え
git remote set-url origin git@github.com:correct-user/correct-repo.git
# 別名リモートを追加
git remote add upstream https://github.com/original/repo.git
# 不要なリモート削除
git remote remove old-origin
# HTTPS ↔ SSH 切替
# HTTPS → SSH
git remote set-url origin git@github.com:user/repo.git
# SSH → HTTPS
git remote set-url origin https://github.com/user/repo.git
対処4: SSH 鍵が未登録
Permission denied (publickey) が併発する場合:
# 既存鍵を確認
ls -la ~/.ssh/
# 無ければ生成 (ed25519 推奨)
ssh-keygen -t ed25519 -C "you@example.com"
# 保存先はデフォルト Enter で OK
# passphrase は CI なら空でも可
# 公開鍵を GitHub Settings → SSH and GPG keys に貼り付け
cat ~/.ssh/id_ed25519.pub
# ssh-agent に追加
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
# 疎通テスト
ssh -T git@github.com
# Hi <username>! You've successfully authenticated, ...
# SSH 設定で複数アカウント使い分け
cat >> ~/.ssh/config <<'EOF'
Host github.com-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work
EOF
対処5: HTTPS / PAT 認証エラー
2021 年 8 月以降、GitHub は HTTPS のパスワード認証を廃止しました。Personal Access Token を使います:
# git の credential 設定
git config --global credential.helper cache
# またはストア永続化(macOS)
git config --global credential.helper osxkeychain
# 初回 push 時にユーザー名とトークン (パスワードの代わり) を入力
git push
# Username: your-github-username
# Password: ghp_xxxxxxxxxxxxxxxxxxxx ← PAT を入力
# URL に直接埋め込む(非推奨:履歴に残る)
git remote set-url origin https://USER:TOKEN@github.com/user/repo.git
# Personal Access Token 作成
# GitHub → Settings → Developer settings → Personal access tokens
# スコープ: repo (private 利用時) / public_repo
対処6: 社内 Git サーバ / VPN / プロキシ
# DNS 解決確認
nslookup git.internal.example.com
# 疎通確認
curl -I https://git.internal.example.com
ping git.internal.example.com
# VPN 接続必要なら接続後に再試行
# プロキシ経由(社内 PC)
git config --global http.proxy http://proxy.example.com:8080
git config --global https.proxy http://proxy.example.com:8080
git config --global http.sslVerify false # 自己署名のみ(非推奨)
# 一時的に無効化
git -c http.proxy= push
初心者がハマる典型パターン
| 状況 | 原因 | 対処 |
|---|---|---|
サブディレクトリで git init したのを忘れている | 親ディレクトリでコマンド実行 | cd でリポジトリ内へ |
| VS Code で「フォルダを開く」したが git 管理外 | .git が無い | Source Control → Initialize Repository |
クローン直後にディレクトリ外で git status | カレントが repo の親 | cd repo |
| Windows でパスの大文字小文字 | git は大小区別する | 正しい URL を貼り直し |
FAQ
Q: git clone したのにすぐ「not a git repository」と言われる
A: クローンしたディレクトリの外で実行しています。cd <cloned-dir> してから操作してください。
Q: 自分は read 権限あるはずなのに「Repository not found」
A: PAT のスコープが古い可能性。GitHub Settings から PAT を再発行し、repo スコープを付与してください。Organization の SSO 認可も必要なことがあります。
Q: WSL から Windows の git に切り替えたら毎回認証要求
A: git config --global credential.helper "/mnt/c/Program\ Files/Git/mingw64/libexec/git-core/git-credential-manager.exe" で Windows 側の認証情報を共有できます。