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?

リモートに存在しないローカルブランチを削除する方法

Posted at

前置き

普段git操作はIDEでぽちぽちしているけど、爆速で増えていくブランチの削除が非常に面倒。

コマンド一発で快適に作業したいので、やり方を残しておく。

コマンド

git fetch -p && git branch -vv | grep ': gone]' | awk '{print $1}' | xargs git branch -D

処理の流れ

  1. git fetch -p
    • リモートの最新状態を取得
    • -p(prune)でリモートから消えたブランチの追跡情報をローカルから削除。
  2. git branch -vv
    • ローカルブランチ一覧を詳しく表示して、各ブランチがどのリモートブランチを追跡しているかを確認。
    • goneがついていると、「リモートにもう存在しない」
  3. grep ': gone]'
    • goneとタグづけされている行だけを抽出する。
    • やってることは「リモートで削除されているブランチ」の一覧
  4. awk '{print $1}'
    • 抽出された行からブランチ名だけ取り出す
  5. xargs git branch -D
    • 取り出したブランチ名をまとめて渡して強制削除
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?