はじめに
Masterブランチにマージ済みのローカルブランチを、毎回毎回手動で削除するのは手間なので、コマンド1行で削除します。
動作環境
- Windows10 Pro
- Git bash
実現方法
$ git checkout master && git pull && git branch --merged | grep -v '*' | xargs -I % git branch -d %
Masterブランチではなく、developブランチにマージしたものを削除する場合は、ブランチ名を適宜変更してください。
詳細
git checkout master
- Masterブランチに移動します
git pull
- Masterブランチを最新化します
git branch --merged
- Masterブランチにマージ済みのブランチを表示します
grep -v '*'
- Masterブランチ
* master
を除外します
- Masterブランチ
xargs -I %
- 上記のgrep結果を標準入力パラメータに渡します
git branch -d %
xargs
に渡された標準入力パラメータを使って、ブランチを削除します
実行例
本コマンド実行前のローカルブランチ達です。カオスです。
$ git branch X0005 bugfix/#565 bugfix/30041 bugfix/30063 bugfix/30085 bugfix/30086 bugfix/30098 bugfix/30119 bugfix/30121 bugfix/40013 bugfix/40018 bugfix/40019 bugfix/40020 bugfix/50001 bugfix/60002 bugfix/60007 bugfix/60007-2 bugfix/60008 bugfix/X0003 bugfix/X0006 bugfix/X0008 bugfix/X0009 bugfix/X0011 ・・・略・・・ * master
本コマンド実行後。スッキリしましたね!
$ git branch * master
おわりに
開発が終わったfeatureブランチや、bugfixブランチをそのままにしておくと、ローカルのGitの操作性が落ちるため、マージしたブランチはどんどん削除していきましょう。