LoginSignup
5
8

More than 3 years have passed since last update.

リモートブランチを確認するために、リモートブランチにgit switchしたい

Last updated at Posted at 2020-07-11

なぜgit switchを使うか?

オシャレだからです

結論

以下コマンドを打ちます。
originなどのリモートブランチを指定しないのがポイントです。

$ git fetch
$ git switch feature/branch01

何をしたか

feature/branch01というブランチで、(例えば)GitHub上にプルリクが作られています。
そのプルリクのレビューをするために、手元(ローカル)で色々確認したいとします。

$ git fetch  #まずはリモートから取得
$ git switch origin/feature/branch01  #そのリモートブランチにswitchしたい
fatal: a branch is expected, got remote branch 'origin/feature/branch01'

え、何このエラー
試しにgit checkoutの方でやります

$ git checkout origin/feature/branch01
Note: switching to 'origin/feature/branch01'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

チェックアウトできたけど、よく見ると「HEADがdetached状態だよ」と警告されてますね

参考: detached HEAD から脱出する方法を git の内部構造から探る

調べた

そもそも、検索した限りでこの方法でリモートブランチを確認する人間はいなかったです。笑

  • この方法ではリモートのorigin/feature/branch01にあるコミットを直接参照している
  • よって、トラッキングもattachもされていない
  • よって、この中で例えば変更を行うと、リモートブランチへの反映は面倒
  • よって、普通はこの方法ではなくローカルブランチにトラッキング・attachさせる方法を使う(推測)
  • よって、git switchではこの方法でのチェックアウトは想定されていない(推測)
  • よって、エラーとなった

のようです。

それで、普通はどう行うのかというと、以下の方法です。

$ git branch feature/branch01 origin/feature/branch01  #リモートブランチからattachさせたローカルブランチを作成し、
$ git checkout feature/branch01  #そのローカルブランチにチェックアウトします(`git switch`でも可能)

ただ、こんな面倒なコマンドを打たないためのショートカットはいろいろあります。
下記のサイトが参考になりました。

参考: git checkout -t でちょっと幸せになれる

結論としては、リモートブランチを(origin)を指定せずにgit checkoutしてしまえば良いみたいです。

やってみた

$ git checkout master  #一回masterに戻り、
$ git branch -D feature/branch01  #前項目で作ったブランチを一度消します
$ git checkout feature/branch01  #attachされたローカルブランチを作って、さらにチェックアウトします
Updating files: 100% (123/123), done.
Branch 'feature/branch01' set up to track remote branch 'feature/branch01' from 'origin'.
Switched to a new branch 'feature/branch01'

できました。
ちなみにgit switchでやっても同じ結果でした。

$ git switch master
$ git branch -D feature/branch01
$ git switch feature/branch01
Updating files: 100% (123/123), done.
Branch 'feature/branch01' set up to track remote branch 'feature/branch01' from 'origin'.
Switched to a new branch 'feature/branch01'

(再度)結論

以下コマンドを打ちます。
originなどのリモートブランチを指定しないのがポイントです。

$ git fetch
$ git switch feature/branch01

感想

個人的には、確認したいだけなのにローカルブランチが作られるのは微妙じゃないか?と思ってます。
レビューが終わった後にいちいちローカルブランチ消すの面倒だし・・・
ご指摘お待ちしております。

参考

fatal: a branch is expected, got remote branch 'origin/feature/branch01'

このエラーで調べると下記質問が出てきました。やはり-cオプションでattachさせるやり方を推奨しています。
https://stackoverflow.com/questions/58124219/how-can-i-use-the-new-git-switch-syntax-to-create-a-new-branch

5
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
8