English

Gitのorigin does not appear to be a git repositoryの直し方

​​Git​コマンドライン,トラブルシュート

git push origin main で fatal: 'origin' does not appear to be a git repository と出たら、そのリポジトリには origin という名前のリモートが登録されていません。git remote -v で登録済みの名前を確かめ、git remote add で登録するか、git remote rename で名前を直すと、同じコマンドが通るようになります。

新しく作ったリポジトリを GitHub などへ初めて push するときや、リモートの名前を打ち間違えたときに出るエラーです。リモートを登録していないときや、コマンドで名前を打ち間違えたときなど、5つの場合で、実際にエラーを出してから直しました。

補足

検証環境は Ubuntu 24.04、Git 2.43.0、bash 5.2 です。英語のメッセージにそろえるため LANG=C で実行しました。リモートには、同じ検証環境の中に作ったベアリポジトリ(../server/app.git など)を使っています。GitHub などのサービスへの接続や、Windows・macOS での表示は試していません。出力に出る作業フォルダーのパスは /path/to/… に置き換えています。

git remote -v の表示ごとの直し方の図。何も表示されないならリモートが未登録なので git remote add origin URL、upstream など別の名前だけなら git push upstream main か git remote rename upstream origin、orgin などつづりの違う名前なら git remote rename orgin origin、origin があるならコマンドの打ち間違いなので正しい名前で実行し直す。引用符の中が URL やパスなら、登録した URL の誤りなので git remote set-url で直す
git remote -v の表示で、直し方が決まる

リモートを登録していないと出る

git init で作ったばかりのリポジトリには、リモートがありません。git remote -v を実行しても、何も表示されませんでした。この状態で origin に push すると、次のメッセージが出ます。終了コードは 128 です。

console
$ git remote -v

$ 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.

git push -u origin main、git fetch origin、git pull origin main でも、まったく同じ文面でした。git pull だけは、終了コードが 1 でした。

直し方:git remote add で origin を登録する

git remote の公式リファレンスにあるとおり、git remote add <名前> <URL> で、その URL のリポジトリを、指定した名前のリモートとして登録します。origin という名前で登録すると、同じ git push -u origin main が通りました。

console
$ git remote add origin ../server/app.git

$ git remote -v
origin	../server/app.git (fetch)
origin	../server/app.git (push)

$ git push -u origin main
To ../server/app.git
 * [new branch]      main -> main
branch 'main' set up to track 'origin/main'.

ここでは URL の代わりに、検証環境の中のフォルダー(../server/app.git)を指定しています。実際には、push したいリモートのリポジトリの URL を指定します。git remote の使い方そのものは、別の記事「git remoteで、リモートの登録・URL変更・削除をする方法」で扱っています。

なぜ「リポジトリではない」と言われるのか

git push の公式リファレンスによると、push 先に指定する <repository> には、URL とリモートの名前のどちらも書けます。Git のソース(remote.c)を見ると、指定した名前のリモートが登録されていないときは、その名前をそのまま URL として扱っています。

そのため、origin が登録されていないと、Git は origin というフォルダーをリポジトリとして開こうとします。開けないので、'origin' does not appear to be a git repository(origin は Git のリポジトリではないようです)と出ます。

本当にその名前のフォルダーがあると、push はそこに届きます。次の例では、リモートとしては登録していない orign という名前で、作業フォルダーの中にベアリポジトリを作ってから push しました。エラーにならず、To orign と出て、そのフォルダーに main ができました。

console
$ git init -q --bare orign

$ git push orign main
To orign
 * [new branch]      main -> main

$ git -C orign log --oneline main
e983eb7 最初のコミット

確かめたあと、この orign フォルダーは消しました。次の節の例は、このフォルダーがない状態で実行しています。

この動きのため、メッセージの ' ' の中には、リモートの名前が出ることも、URL が出ることもあります。どちらが出ているかで、直す場所が変わります(この記事の最後の節で扱います)。

打ち間違えた名前が、そのままメッセージに出る

origin を登録したあとでも、コマンドで名前を打ち間違えると、同じ形のエラーになります。' ' の中には、打ち間違えた名前(orign)が出ます。

console
$ git push orign main
fatal: 'orign' 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.

$ git remote
origin

git remote は、登録してあるリモートの名前だけを一覧にします。ここに origin が出ているなら、リモートの設定は正しいので、コマンドを正しい名前で実行し直せば通ります。git remote get-url に名前を渡すと、登録されていない名前では error: No such remote 'orign' と出るので、名前があるかどうかをはっきり確かめられます。

console
$ git remote get-url orign
error: No such remote 'orign'

$ git remote get-url origin
../server/app.git

リモートの名前が origin ではない

origin は、git clone が付ける名前です。git clone の公式リファレンスのとおり、--origin(-o)を付けて clone すると、ほかの名前になります。-o upstream で clone したリポジトリでは、origin に push するとエラーになりました。

console
$ git remote -v
upstream	/path/to/server/app.git (fetch)
upstream	/path/to/server/app.git (push)

$ 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.

直し方は2つあります。名前はそのままにして、git push upstream main のように登録してある名前で push しても通ります。

console
$ git push upstream main
To /path/to/server/app.git
   e983eb7..b708781  main -> main

いつもの origin で push したいなら、git remote rename で名前を変えます。公式リファレンスによると、名前を変えると、リモート追跡ブランチ(upstream/main など)とそのリモートの設定も、新しい名前に合わせて書き換わります。実際に、main の追跡先も origin/main に変わり、git push origin main が通りました。

console
$ git remote rename upstream origin

$ git remote -v
origin	/path/to/server/app.git (fetch)
origin	/path/to/server/app.git (push)

$ git branch -vv
* main b708781 [origin/main] 2つめのコミット

$ git push origin main
To /path/to/server/app.git
   b708781..9632c5f  main -> main

remote add で名前を打ち間違えた

git remote add のときに名前を打ち間違えると、リモートは登録されているのに、origin は見つかりません。git remote -v を見ると、orgin という名前で登録されていました。

console
$ git remote add orgin ../server/app3.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.

$ git remote -v
orgin	../server/app3.git (fetch)
orgin	../server/app3.git (push)

この場合も、git remote rename で正しい名前に変えれば、登録し直さずに済みます。push 先には、ベアリポジトリ ../server/app3.git を作っておきました。

console
$ git remote rename orgin origin

$ git push origin main
To ../server/app3.git
 * [new branch]      main -> main

メッセージに URL が出ているときは、登録した URL が違う

' ' の中が名前ではなく URL やパスのときは、名前は正しく、登録した URL の先にリポジトリがありません。origin の URL を、ないフォルダー(../server/nai.git)に変えて push すると、' ' の中にその URL が出ました。

console
$ git remote set-url origin ../server/nai.git

$ git push origin main
fatal: '../server/nai.git' 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.

この場合は名前を直しても解決しないので、git remote set-url で正しい URL に直します。直したあとは、push が通りました(すでに送ってあったので Everything up-to-date と出ています)。

console
$ git remote set-url origin ../server/app3.git

$ git push origin main
Everything up-to-date

​Posted by 86