0
0

More than 1 year has passed since last update.

【Git】ローカルブランチを他のリモートブランチに切り替える手順

Posted at

概要

Git リポジトリを、「develop」等のメインブランチでローカルにクローンした際、後からリモートで用意されている他のブランチに切り替える手順の備忘郎。

前提

  • Git に関する基礎知識がある方向け
  • Git インストール済み

手順

リポジトリが存在する場所で、以降を実施していく

まずローカルブランチを確認

# リモートブランチ一覧表示
git branch

# 実行結果 (* は現在選択されているブランチ)
* develop

初回は、ブランチが1つしかない状態。

目的のブランチがリモートあることを確認

# リモートブランチ一覧表示
git branch -r

# 実行結果 (リモートブランチの一覧が出てくる)
  origin/HEAD -> origin/develop
  origin/develop
  origin/feature/branch_A
  origin/feature/branch_B
  origin/feature/branch_C
 :

ローカルリポジトリ最新化コマンド (情報を取得する)

# フェッチコマンド実行
git fetch

# 実行結果 (こんな感じの表示が出る)
remote: Enumerating objects: 118, done.
remote: Counting objects: 100% (118/118), done.
remote: Compressing objects: 100% (44/44), done.
remote: Total 118 (delta 85), reused 106 (delta 73), pack-reused 0
Receiving objects: 100% (118/118), 16.29 KiB | 8.14 MiB/s, done.
Resolving deltas: 100% (85/85), completed with 13 local objects.
From github.com:Demo-project/demo-app
   d9f54d9a5..c2f1d2cfa  develop          -> origin/develop
 * [new branch]          feature/branch_A -> origin/feature/branch_A
 + 1af70c600...75974fa96 feature/branch_B -> origin/feature/branch_B  (forced update)

ブランチを切り替える

ブランチ作成、リモートからローカルへの適用、切り替えを同時に行う
(ブランチ名の指定には「 origin/ 」は不要)

# ブランチ切り替え
git checkout feature/branch_B

# 実行結果
Branch 'feature/branch_B' set up to track remote branch 'feature/branch_B' from 'origin'.
Switched to a new branch 'feature/branch_B'

ローカルブランチを確認

# リモートブランチ一覧表示
git branch

# 実行結果 (* は現在選択されているブランチ)
  develop
* feature/branch_B

選択したブランチに切り替わっていることを確認できる

まとめ

基本は「 git checkout {ブランチ名} 」をやれば良いが、念のため fetch コマンドで最新かなどもしてみた手順

0
0
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
0
0