一撃で理解できるgit branch 〇〇集

2024/04/18に公開

概要

git branchのコマンドはよく使います。
いろんなオプションなどがあるので知っていると便利なので記事にして見ました。

git branch

オプションも何もつけない場合はローカルのブランチの一覧を表示します。
あと自分が今いるブランチも確認できます。

% git branch

-a 、--all

リモートブランチとローカルブランチを全部表示します。

 % git branch -a
 % git branch --all

-r 、--remote

リモートブランチ一覧を表示します。

% git branch --r
% git branch --remote

-v --verbose

各ブランチの最新のコミットメッセージとコミットハッシュを表示します。

% git branch -v
% git branch --verbose

--merged

マージ済みのブランチのみを表示

% git branch --merged 

--no-merged

マージされていないブランチのみを表示します。

% git branch --no-merged

組み合わせて使う

例えば-avにすると
ローカルブランチとリモートブランチのすべてのブランチを詳細な情報とともに表示します。

% git branch -av

ブランチ作成

% git branch ブランチ名
% git branch -c ブランチ名
% git branch --copy ブランチ名

ブランチ削除

% git branch -d ブランチ名
% git branch --delete ブランチ名

指定したブランチを強制的に削除します。

% git branch -D ブランチ名

現在のブランチ名を変更

% git branch -m ブランチ名
% git branch --move ブランチ名

変更元を指定してします。

% git branch -m 古いブランチ名 新しいブランチ名

リモートブランチをローカルに追跡する新しいブランチを作成

% git branch  -t <remote>/<branch>
% git branch --track <remote>/<branch>

現在のブランチと指定したアップストリームブランチの間に追跡関係を設定

% git branch -u <upstream>
% git branch --set-upstream-to=<upstream>

--help

使用できるオプションのリストと詳しい説明が表示されます。

% git branch --help

資料

https://qiita.com/chihiro/items/e178e45a7fd5a2fb4599

補足

2023年07月16日に投稿した記事です。

Discussion