やけに丁寧なtigのキーバインド設定ガイド で、リモートブランチを取得する設定が紹介されていた。
コマンドラインから叩くよりは楽なんだけど、checkout 先を入力する必要があるのがダルい。
そこで次のように設定してみた。
bind branch r !sh -ec 'b=`expr "%(branch)" : "origin/\(.*\)"`; git checkout -b $b %(branch)'
%(branch)
で取れるブランチ名は origin/feature/test
のようになっていて、除去したいので、!sh で shell を起動し、expr を経由させて、それをやっている。
更に、origin/ で始まっていない branch は対象外としたいので sh に -e オプションをつけている。
追記
-t/--track オプションを使うともっと簡単にできることをコメント欄で教えてもらった。
bind branch r ...
は次のように書ける:
bind branch T !git checkout -t %(branch)
使い方
tig を起動し、H
を押して、ブランチビューに移動する。
そしてチェックアウトしたいブランチにカーソルを移して、r
を押す。
すると一旦以下のようになるので、言われたとおりに Enter
を押す。
feature/test
がチェックアウトできた!
ちなみに、origin/ で始まっていないブランチ (master branch とか) で r
を実行しても、何もしない。