0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

リモートで消滅したブランチを上流にしていたローカルブランチを削除する

Posted at

TL;DR

alias git-gone="git fetch --prune && git branch --format '%(refname:short) %(push:track) %(HEAD)'|awk '\$NF==\"[gone]\"{print \$1}'|xargs -t -r git branch -D"

やりたいこと

  • PRの受け入れなどでリモートで削除されたブランチに対応するローカルブランチを削除したい
    • git fetch --prune 等でリモート追跡ブランチは削除できる
    • それらをupstreamにしていた作業用のローカルブランチが残る

やったこと

# 最新を取ってきて追跡ブランチの削除
git fetch --prune \
# 欲しい情報を整形して取得。以下のどちらかの出力を期待する
# branch/name [tracking_status]
# branch/name [tracking_status] * ※現在チェックアウト中のブランチの場合
&& git branch --format '%(refname:short) %(push:track) %(HEAD)' \
# 最後のフィールドが [gone] になっているブランチ名を取得
# チェックアウト中のブランチは最後に * が来るので外れる
|awk '$NF==\"[gone]\"{print $1}' \
# 全ブランチを強制削除。goneしてるのに未pushがある?知らんがな。
|xargs -t -r git branch -D"

上記をエイリアス定義して利用することにした。

p.s.

同じことができるコマンドがあれば教えていただけると助かります。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?