できない.dev

Git fetch しても新しいリモートブランチが見えない

`git fetch` で新しいリモートブランチが反映されないのは、`--prune` 不足で参照が古い・refspec が狭い・別 remote のブランチを見ようとしているの 3 系統。
`git fetch --all --prune` と `git branch -r` で大半は切り分けられる。

#fetch#remote#branch#prune#tracking

公開:

要約

git fetch で新しいリモートブランチが見えないときは、(1) --prune 不足で参照が古い (2) refspec が狭い (3) そもそも別 remote のブランチを見ようとしている の 3 点を疑う。
最初に git fetch --all --prune を実行し、git branch -r で取得結果を確認するのが最短ルート。

よくある原因

  1. --prune を付けていない: リモートで削除されたブランチがローカルに残り、新規ブランチと混ざって見落とす
  2. refspec が狭い: remote.origin.fetch が 1 ブランチに固定されていると他ブランチは取得対象外
  3. 別 remote のブランチ: fork した upstream のブランチは git remote add していなければ見えない
  4. ローカルとリモート追跡を混同: git branch だけではローカルしか出ない。
    リモート分は git branch -r で見る
  5. そもそも push されていない: 同僚がローカルブランチを作ったまま push 前で、サーバ側にまだ存在しない

解決策

1. まず --all --prune で取り直す

git fetch --all --prune
git branch -r

--prune は削除済みリモート参照を整理する。--all は登録済みの全 remote を一括で fetch する(git-fetch 公式 参照)。

2. remote と refspec を確認する

git remote -v
git config --get-all remote.origin.fetch

期待値は +refs/heads/*:refs/remotes/origin/*
これ以外(例: +refs/heads/main:refs/remotes/origin/main)になっていると 1 ブランチしか取得しない。

3. upstream を追加する

fork 元のブランチを参照したいときは、追加 remote として登録する。

git remote add upstream https://github.com/ORG/REPO.git
git fetch upstream --prune
git branch -r

git remote の運用全般は公式リファレンスに詳しい。

4. ローカルにブランチを作って追跡する

git switch feature/new-branch

同名リモート追跡ブランチがあれば、ローカルブランチを自動で作成し追跡する。

この記事は役立ちましたか?