LoginSignup
11
10

More than 3 years have passed since last update.

git checkoutするブランチ名をpecoで選択する git エイリアス

Posted at

ブランチ選択面倒ですよね

  • git branch して、あるいは git branch --remote してブランチ一覧を確認
  • ブランチ名をコピーして、remoteブランチだったら remotes/origin/ などのプレフィックスを外して
  • git checkout

そんなゴニョゴニョは peco に任せましょう。

前提条件

  • peco コマンドがインストールされていること
  • tr, grep , sed, xargs などのunixでは標準的なコマンドが使える環境であること

alias に設定するコマンド

[alias]
    cop = !"git branch --all | tr -d '* ' | grep -v -e '->' | peco | sed -e 's+remotes/[^/]*/++g' | xargs git checkout"

コマンドの詳細

git branch --all

  • remoteブランチも含めすべてのブランチを出力(ただし、 git fetch していないなどでremoteブランチの情報が同期されていないと表示されません)

tr -d '* '

  • * (半角スペース)を削除します

grep -v -e '->'

  • -> を含む行を削除します。 remotes/origin/HEAD -> remotes/origin/master などの行を除外するためです。

peco

  • 入力された情報を使ってインクリメンタルサーチと選択を行います

sed -e 's+remotes/[^/]*/++g'

  • 選択された行が remotes/origin/feature/function 等の場合、 remotes/origin/ の部分を削除して具体的な checkout 用のブランチ名にします。
  • 人によってはremotesの名前に / を含んだりするのかもしれないけど…一旦無視してます。

xargs git checkout

  • 選択されたブランチ名を使ってチェックアウトを行います

cop = !"<command>"

  • そもそもこの alias の書き方はなんぞ?と思う人もいるかと思いますが、単純にこれをシェルとして実行するという意味です。

alias設定しましょう

色々できるんで、よく使うコマンドがあるひとはぜひ。

11
10
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
11
10