LoginSignup
0
0

More than 3 years have passed since last update.

Gitでブランチをまとめて消すシンプルな方法

Posted at

Gitで特定の名前のブランチ群をまとめて消したい時があります。xargsやgrepを使う方法もありますが、ここではgitコマンドのみでできるシンプルな方法をまとめます。

確認環境

  • MacBook Pro (16-inch 2019)
  • macOS Catalina 10.15.6

方法

例えば名前の頭にdevelop/のついたブランチを消したいとします。
その場合、以下をコマンドラインで実行します。

git branch -D `git branch --list 'develop/*'`

これの原理は簡単で、まず消したいブランチを名前で絞り込み(git branch --list 'develop/*')、それを削除コマンド(git branch -D <削除対象>)へ送っています。

より安全な手順

上記コマンドをそのまま実行すると、もし絞り込みの指定でミスっていた場合、必要なブランチを消してしまう可能性があります。
まずは以下のコマンドを単独で実行して、表示されたブランチを確認し、

git branch --list 'develop/*' # 削除対象を確認

それらが消えてよければ、実際の削除に進むのが良いと思います。

git branch -D `git branch --list 'develop/*'` # 実際の削除

以上です。

参考

Can you delete multiple branches in one command with Git?
https://stackoverflow.com/a/47304256

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