2.

git「fatal: '...' does not appear to be a git repository」の原因と対処

編集
この記事の要点
  • git clone / push / pull / fetch 時に出る基本エラー「does not appear to be a git repository
  • 主な原因: ① そもそも .git ディレクトリが無い場所で実行 / ② リモート URL のタイポ / ③ SSH 鍵未登録 / ④ git remote add origin 未実行
  • 対処: git status / git remote -v で状態確認 → git init / git remote add origin
  • プライベートリポジトリで存在しないように見える場合は権限不足の可能性 → PAT / SSH 鍵で認証
  • 社内 Git サーバの場合はVPN 未接続や DNS 解決失敗もよくある

このエラーの概要

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 .gitgit init(対処1)
origin 未設定git remote -vgit remote add origin URL(対処2)
URL タイポgit remote get-url origingit remote set-url(対処3)
SSH 鍵未登録ssh -T git@github.com鍵登録(対処4)
権限不足ブラウザで該当 URL を開く招待 / トークン更新(対処5)
VPN/プロキシping git.internal / curl -I URLVPN 接続(対処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 ! 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 してから操作してください。

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 側の認証情報を共有できます。

編集
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: