English

Gitのcouldn’t find remote refを直す方法

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

git pull origin feature/logn のようにブランチ名を指定して fatal: couldn't find remote ref feature/logn と出たら、その名前のブランチがリモートにありません。git ls-remote --heads origin でリモートにあるブランチの名前を確かめ、正しい名前で pull し直せば消えます。

ブランチ名の打ち間違い、main と master の取り違え、リモートで消されたブランチの pull などで出るエラーです。検証環境でエラーを起こしてメッセージを確かめ、名前を直すと消えるところまで試しました。

補足

検証環境は Ubuntu 24.04、Git 2.43.0、bash 5.2 です。リモートには、同じ検証環境に作った裸のリポジトリを使い、最初は main と feature/login の2つのブランチを置きました。英語のメッセージにそろえるため LANG=C で実行しています。出力に出るリモートのパスは /path/to/… に置き換えています。GitHub などのホスティングサービスからの pull や、Windows・macOS での表示は試していません。

存在しないブランチを pull すると出るメッセージ

リモートにある feature/login を、i を抜かして feature/logn と打って pull しました。出たのは次の1行だけで、終了コードは 1 です。

console
$ git pull origin feature/logn
fatal: couldn't find remote ref feature/logn

pull の前後で git log --oneline -1 を比べると、どちらも f39804d 最初のコミット のままでした。何も取り込まれず、手元のブランチは変わっていません。git fetch origin feature/logn でも同じメッセージが出ました(こちらの終了コードは 128 です)。

大文字と小文字の違いや、main と master の取り違えでも出ます。リモートには main はありますが、Main や master はないので、どちらを指定しても止まりました。

console
$ git pull origin Main
fatal: couldn't find remote ref Main

$ git pull origin master
fatal: couldn't find remote ref master

リモートにあるブランチと名前が一致しないと止まる

git pull origin <名前> は、まずリモートからブランチやタグの一覧を受け取り、その中から指定した名前を探します。Git のソース(remote.c)では、名前に合うものが見つからなければ、このメッセージを出して止めています。

エラーが出る仕組みの図。git pull origin feature/logn は、いまのリモートにある main と feature/login から名前を探し、同じ名前がないので fatal: couldn't find remote ref feature/logn で止まる。名前は git ls-remote --heads origin で確かめる。git branch -r は前回 fetch したときの記録で、消えたブランチも残る
指定した名前がリモートにないと、何も取り込まずに止まる

リモートにあるブランチの名前を確かめる

git ls-remote に --heads を付けると、リモートにあるブランチの一覧が出ます(git ls-remote の公式リファレンス)。refs/heads/ の後ろが、pull で指定する名前です。

console
$ git ls-remote --heads origin
1f1a799d078e32c00dd6b899a3327179885ed93c	refs/heads/feature/login
f39804d63cee12a9dd96e457487f130c25b2b37f	refs/heads/main

ブランチが多いときは、'*login*' のように、名前の一部を * で挟んだパターンを渡すと絞り込めます。パターンはシェルに展開されないよう、引用符で囲みます。

console
$ git ls-remote --heads origin '*login*'
1f1a799d078e32c00dd6b899a3327179885ed93c	refs/heads/feature/login

git remote show origin でも、Remote branches: の欄にリモートのブランチが出ました。git remote の公式リファレンスによると、show も -n を付けなければ、リモートに問い合わせてから表示します。

console
$ git remote show origin
* remote origin
  Fetch URL: /path/to/remote.git
  Push  URL: /path/to/remote.git
  HEAD branch: main
  Remote branches:
    feature/login tracked
    main          tracked
(以下、省略)

正しい名前で pull し直すとエラーが消える

確かめた名前の feature/login で pull し直すと、エラーは出ずに取り込まれました。

console
$ git pull origin feature/login
From /path/to/remote
 * branch            feature/login -> FETCH_HEAD
Updating f39804d..1f1a799
Fast-forward
 login.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 login.txt

master を指定して止まった場合も、main に直すと Already up to date. と出て、エラーは消えました。

git branch -r に残っていても、リモートにあるとは限らない

git branch -r の一覧に origin/feature/old があるのに、git pull origin feature/old で同じエラーが出ることがあります。git branch -r が表示するのは、前回 fetch したときに手元に記録したリモートのブランチで、リモートでブランチが消されても、そのまま残るためです。

別のクローンから feature/old をリモートで消したあとに試すと、git branch -r には origin/feature/old が出たままで、pull は止まりました。

console
$ git branch -r
  origin/HEAD -> origin/main
  origin/feature/login
  origin/feature/old
  origin/main

$ git pull origin feature/old
fatal: couldn't find remote ref feature/old

git fetch --prune を実行すると、git fetch の公式リファレンスのとおり、リモートにもうないブランチの記録が手元から消え、git branch -r からも消えました。リモートで消えたブランチの片付け方は、別の記事「git fetch –pruneでリモートで消えたブランチを片付ける」で詳しく扱っています。

console
$ git fetch --prune origin
From /path/to/remote
 - [deleted]         (none)     -> origin/feature/old

$ git branch -r
  origin/HEAD -> origin/main
  origin/feature/login
  origin/main

引数なしの git pull では、別のメッセージになる

上流(upstream)に設定したブランチがリモートで消えていて、引数なしで git pull を実行したときは、couldn't find remote ref ではなく、次のメッセージで止まりました。終了コードは 1 です。

console
$ git pull
Your configuration specifies to merge with the ref 'refs/heads/feature/cart'
from the remote, but no such ref was fetched.

feature/cart の上流は origin/feature/cart のままですが、リモートにはもう feature/cart がありません。git fetch --prune のあとに git branch -vv を見ると、上流に gone と出ていました。

console
$ git branch -vv
* feature/cart 2d169fd [origin/feature/cart: gone] カート
  main         1f1a799 [origin/main: ahead 1] ログイン画面

git branch -u で、これから取り込みたいリモートのブランチを上流に付け替えると直ります。git branch の公式リファレンスにあるとおり、ブランチ名を省くと、いまのブランチの上流が変わります。ここでは main の変更を取り込む場合として origin/main にしたところ、git pull は Already up to date. と出て、止まらなくなりました。

console
$ git branch -u origin/main
branch 'feature/cart' set up to track 'origin/main'.

$ git pull
Already up to date.

上流を main にすると、以後の引数なしの git pull は main をこのブランチに取り込みます。このブランチではもう pull しないなら、付け替えずに git branch --unset-upstream で上流を外す方法もあります。上流の付け替え方や外し方は、別の記事「Gitでブランチの上流(upstream)を設定する方法」で扱っています。

​Posted by 86