LoginSignup
16
9

More than 5 years have passed since last update.

gitでブランチやタグを一括削除する

Posted at

git bashを使います(windows環境だと通常gitインストール時に同梱されているはず)

xargsコマンドを使う

標準入力から受け取って、任意のコマンドに引数として渡すコマンド

ローカルブランチを一括削除する

git branch | xargs git branch -D
これで現在のブランチ以外強制で消せる
タグも同じ要領で消せる
git tag | xargs git tag -d

masterとdevelop以外を消す

grepコマンドを使う

ファイル中の文字列を検索するコマンド

git branch | grep -vE '^\*|master$|develop$' | xargs -I % git branch -D %
-vは一致しないものを検索するオプション
-Eは拡張正規表現を使うオプション

辞書登録しておくと便利なのでおすすめです:grinning:

リモートにないローカルリモート追跡ブランチを消す

git fetch -p
-p は--pruneの略

リモートにあるブランチを消す

git push origin :ブランチ名
空の箱をリモートにプッシュして消すイメージ
コロンの左側にローカルブランチ右側にリモートの対象ブランチ
githubだと画面でゴミ箱のアイコンを押せばいい

リモートにあるブランチを一括で消す

cut コマンドを使う

指定した部分だけを切り出すコマンド

originなら下記で行けるはず

git branch -r | cut -c 10- | xargs -I % git push origin :%
branch -r でリモートの一覧を出すが
リモート名が邪魔なのでcutで消す。

git branch -r | cut -c ??-

でどうなるか結果を確認したら良い
まぁ使うことなさそう

16
9
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
16
9