created: 2024-12-31T04:29:16.374Z

マージされてないブランチをヒストリを見ながら掃除する

年末の大掃除で、 fzf をつかって、ブランチのヒストリを見ながら削除一覧をみつくろえるコマンドを作った。

  • --bind "space:toggle" でスペースキーで削除リストに追加できる
  • ↑ ↓ キーでブランチの history を見ながら選択
  • enter キーで、選んでたブランチ名が /tmp/deletable-branches.txt に出力される

念の為 git config user.name で自分のブランチに絞っている。

git for-each-ref --format='%(committerdate:short) %(refname:short) %(authorname)' refs/remotes/origin \
    | grep -E "$(git config user.name)$" \
    | sort \
    | fzf --ansi --multi \
        --delimiter " " \
        --bind "space:toggle" \
        --preview 'git log {2} \
            --oneline \
            --pretty="format:%ad %an %h %s" \
            --date=short \
            --color=always
        ' \
    | tee /tmp/deletable-branches.txt

あとはリストのものをなんかして消す。

$ cat /tmp/deletable-branches.txt \
    | cut -wf2 | cut -d/ -f2- \
    | xargs -I{} git push --delete origin {}

ちなみに

すでにマージ済みのブランチはこんな感じで一覧できる。

git branch --remotes --merged \
    | grep -Ev "(^\*|^\+|master|main)" \
    | cut -d/ -f2- \
    | tee /tmp/_.txt
勝間式食事ハック
[ad] 勝間式食事ハック
勝間和代 (Kindle版)