Gitでファイルをプッシュしようとしたとき、次のようなエラーが表示されることがあります。
$ 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.
本記事では、このエラーの代表的な原因6パターンと、それぞれの解決方法を実際のコマンドとともに解説します。GitHubで新規リポジトリを作成してpushする完全手順も掲載しています。
エラーの主な原因
「does not appear to be a git repository」エラーが発生する原因は、大きく以下の6パターンです。
- リモートリポジトリ(origin)がそもそも設定されていない
- remote名のタイプミス(
orginなど) .gitフォルダが存在しない/壊れている- Gitリポジトリの親フォルダで操作してしまっている
- リモートのURLが間違っている
git initをまだ実行していない
解決方法1:git remote -v でリモートの設定を確認する
まず現在のリモート設定を確認しましょう。
git remote -v
origin が設定されている場合の出力例:
origin https://github.com/yourusername/your-repo.git (fetch) origin https://github.com/yourusername/your-repo.git (push)
何も出力されない場合:
(何も表示されない)
解決方法2:git remote add origin でリモートを追加する
originが設定されていない場合は、git remote add コマンドでリモートリポジトリを追加します。
HTTPS形式で追加する場合
git remote add origin https://github.com/yourusername/your-repo.git
SSH形式で追加する場合
git remote add origin git@github.com:yourusername/your-repo.git
追加後、再度 git remote -v で確認します。
git remote -v
origin https://github.com/yourusername/your-repo.git (fetch) origin https://github.com/yourusername/your-repo.git (push)
解決方法3:remote名のタイプミスを修正する
origin のつもりで orgin や origni と登録してしまっているケースがあります。
git remote -v
orgin https://github.com/yourusername/your-repo.git (fetch) orgin https://github.com/yourusername/your-repo.git (push)
この場合は git remote rename で名前を修正します。
git remote rename orgin origin
修正後に確認:
git remote -v
origin https://github.com/yourusername/your-repo.git (fetch) origin https://github.com/yourusername/your-repo.git (push)
解決方法4:git remote set-url でリモートURLを変更する
originは存在するが、URLが間違っている場合は git remote set-url で変更します。
# HTTPS形式に変更 git remote set-url origin https://github.com/yourusername/your-repo.git # SSH形式に変更 git remote set-url origin git@github.com:yourusername/your-repo.git
変更後は git remote -v で確認してから git push を再試行してください。
・
https://github.com/yourusername/your-repo(末尾の .git が抜けている)・
https://github.com/yourUsername/your-repo.git(大文字小文字が違う)・リポジトリ名が変わっているのに古いURLのまま
解決方法5:git init から初期化する
.git フォルダが存在しない場合、そもそもGitリポジトリとして初期化されていないことが原因です。
.gitフォルダの存在を確認する
# Linux / Mac ls -la # Windows(PowerShell) ls -Force # Windows(コマンドプロンプト) dir /a
drwxr-xr-x 8 user staff 256 Mar 18 10:00 .git ← これがあればOK drwxr-xr-x 3 user staff 96 Mar 18 10:00 src -rw-r--r-- 1 user staff 123 Mar 18 10:00 README.md
.git フォルダがなければ、以下のコマンドでGitリポジトリを初期化します。
git init git add . git commit -m "first commit" git remote add origin https://github.com/yourusername/your-repo.git git push -u origin main
git init を実行すると新しいGitリポジトリが作成されます。既存のリポジトリをクローンした場合は、通常 .git フォルダが既に存在するはずです。解決方法6:正しいディレクトリで操作しているか確認する
Gitリポジトリの親フォルダや別のフォルダでコマンドを実行してしまっているケースがあります。
現在のディレクトリを確認する
# 現在地を確認 pwd # 例: /home/user/projects ← リポジトリの親フォルダになっていないか? # 正しいリポジトリフォルダに移動 cd your-repo # .gitフォルダがあるか確認 ls -la | grep .git
・
~/projects/ にいるはずが ~/projects/my-app/ に移動し忘れている・別のリポジトリのフォルダに誤って入ってしまっている
・
git clone した後に cd リポジトリ名 を忘れている正しいフォルダに移動して再度コマンドを実行してください。
cd your-repo git push origin main
GitHubで新規リポジトリを作成してpushする完全手順
「originを設定したことがない」という方向けに、ゼロからGitHubにpushする完全手順を解説します。
手順1:GitHubでリポジトリを新規作成
- GitHubにログインして github.com/new にアクセス
- 「Repository name」にリポジトリ名を入力
- 公開範囲(Public / Private)を選択
- 「Initialize this repository with a README」はチェックしない(ローカルに既にファイルがある場合)
- 「Create repository」をクリック
手順2:ローカルリポジトリを初期化してpushする
# プロジェクトフォルダに移動 cd your-project # Gitリポジトリを初期化 git init # ファイルをステージング git add . # 最初のコミット git commit -m "first commit" # デフォルトブランチをmainに設定 git branch -M main # リモートを追加 git remote add origin https://github.com/yourusername/your-repo.git # pushする(-u で上流ブランチを設定) git push -u origin main
-u オプション(--set-upstream)を付けると、次回以降は git push だけでpushできるようになります。
SSH鍵の設定が済んでいれば、HTTPSの代わりにSSH形式のURLを使えます。
git remote add origin git@github.com:yourusername/your-repo.gitSSH接続の方がトークン管理不要で便利です。
手順3:pushの成功を確認する
Enumerating objects: 3, done. Counting objects: 100% (3/3), done. Delta compression using up to 8 threads Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 1.23 KiB | 1.23 MiB/s, done. Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 To https://github.com/yourusername/your-repo.git * [new branch] main -> main Branch 'main' set up to track remote branch 'main' from 'origin'.
このような出力が表示されれば、pushは成功です。GitHubのリポジトリページを確認してみましょう。
よくある質問(FAQ)
set-url でURLを変更してください。
# 既存のoriginを削除 git remote remove origin # 再度追加 git remote add origin https://github.com/yourusername/your-repo.git
git pull origin main --rebase でリモートの変更を取り込んでからpushしてください。
git pull origin main --rebase git push origin main
詳しくは 【Git】コンフリクトしてpullできないときの対処法完全ガイド も参考にしてください。
git remote set-url origin https://github.com/yourusername/your-repo.git
origin は自分がclone(またはfork)したリモートリポジトリを指します。upstream はfork元の本家リポジトリを指す慣習的な名前です。詳しくは 【Git】originとupstreamの違いと使い分け完全ガイド をご覧ください。git clone した場合は自動的にoriginが設定されるため、通常このエラーは出ません。もし出る場合は、git remote -v でoriginが正しいURLを向いているか確認してください。また、cloneした後に cd リポジトリ名 でフォルダに移動しているかも確認しましょう。詳しくは 【Git】pullで「untracked working tree files would be overwritten by merge」の解決方法 も参考にしてください。まとめ
1. git remote -v でoriginの設定を確認
2. 何も表示されなければ git remote add origin URL で追加
3. typoがあれば git remote rename 誤り origin で修正
4. URLが違えば git remote set-url origin 正しいURL で変更
5. .git フォルダがなければ git init から初期化
6. 正しいフォルダにいるか pwd と ls -la で確認
多くの場合、git remote -v の確認だけで原因が特定できます。
関連記事: