LoginSignup
6
6

More than 5 years have passed since last update.

Pecoでちょっとリッチなgitブランチ選択

Last updated at Posted at 2014-08-06

Pecoが流行っているので使ってみたらよい感じだったので開始してみました。

以下のブログを参考にさせてもらいつつちょっとリッチな表示をするブランチ選択機能です。
* pecoで最近更新されたブランチにcheckoutする

~/.zshrc
function peco-gitbranch() {
    local current_buffer=$BUFFER

    # commiterdate:relativeを commiterdate:localに変更すると普通の時刻表示
    local selected_line="$(git for-each-ref --format='%(refname:short) | %(committerdate:relative) | %(committername) | %(subject)' --sort=-committerdate refs/heads refs/remotes \
        | column -t -s '|' \
        | peco \
        | head -n 1 \
        | awk '{print $1}')"
    if [ -n "$selected_line" ]; then
        BUFFER="${current_buffer} ${selected_line}"
        CURSOR=$#BUFFER
        # ↓そのまま実行の場合
        #zle accept-line
    fi
    zle clear-screen
}
zle -N peco-gitbranch
bindkey '^x^b' peco-gitbranch

上記コマンドを.zshrcに入れておくことで、git checkoutまで打ったあと、^x^bをローカルブランチ、リモートブランチの一覧を詳細(最終更新日時、コミッター、コミットタイトル)付きで表示してpecoに渡します。あとは、絞り込みをして決定。 diffなどにも使えるようにコマンド自体はなかに書いてません。

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