LoginSignup
188
201

More than 5 years have passed since last update.

git branch コマンド

Last updated at Posted at 2017-06-09

20190505追記

  • わかりにくい表現を修正しました。
  • git branch -vコマンドの説明を追加しました。
  • git branch \<branchname>コマンドの説明に追記しました。

git branchコマンドについて、主にオプションをまとめました。

表示

git branch

ローカルブランチの一覧を表示する。

git branch -r

-r、もしくは、--remotesオプション。
リモートブランチの一覧を表示する。

git branch -a

-a、もしくは--allオプション。
リモートブランチを含んだブランチの一覧を表示する。

git branch --merged

--mergedオプション。
HEADにマージ済みのブランチの一覧を表示する。

git branch --no-merged

--no-mergedオプション。
HEADにマージされていないブランチの一覧を表示する。

git branch --color

--colorオプション。
ブランチの一覧の表示をカラフルにする(デフォルト)。

git branch --no-color

--no-colorオプション。
ブランチの一覧を色付きで表示しない。

git branch -v

-v、もしくは--verboseオプション。
詳細情報を付与した状態で、ブランチの一覧を表示する。
各ブランチの先頭のコミットのIDとメッセージを表示する。
-vvオプションを使用することで、追跡しているリモートブランチの名前を表示する。

操作

git branch <branchname>

現在のHEADから、指定したbranchnameを名前として、新しいブランチを作成する。
新しく作成したブランチへの切り替えは行わない。
切り替えを行う場合は、git checkout <branchname> を使用する。
新しいブランチの作成と切り替えを同時に行う場合は、git checkout -b <branchname> を使用する。

git branch -d <branchname>

-d、もしくは、--deleteオプション。
指定したブランチを削除する。
-rオプションを付けた場合は、リモートブランチを削除する。
指定したブランチの内容が、追跡しているリモートブランチ(設定されていない場合はHEAD)にマージされていないと削除できない。

git branch -D <branchname>

-Dオプション。
マージの状態に関わらず、指定したブランチを削除する。

git branch -m <new branchname>

-m、もしくは、--moveオプション。
現在チェックアウトしているブランチ名を<new branchname>に変更する。
git branch -m <old branchname> <new branchname>で変更元を指定することもできる。

git branch -u <upstream>

-u、もしくは、--set-upstream-to=<upstream>オプション。
現在チェックアウトしているブランチが、<upstream>で指定したリモートブランチを追跡するようにする。
git branch -u <upstream> <branchname>で対象ブランチを指定することもできる。

git branch --unset-upstream

--unset-upstreamオプション。
現在チェックアウトしているブランチが追跡しているリモートブランチがあった場合、そのひもづきを解除する。
git branch --unset-upstream <branchname>で対象ブランチを指定することもできる。

188
201
1

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
188
201