2
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.

チーム内で不要とされたremote branchを消すコマンド

Last updated at Posted at 2019-11-29

はじめに

gitを利用している場合
油断するとチリツモの如く溜まっていくremotebranch。

見通しが悪くなったりするので
PRが通ったタイミングで消すのがベストかもしれませんがうっかりなんてことありますよね:disappointed_relieved:

ちょうど試してみたのでその方法の共有になります。

コマンド

git branch -r --merged | egrep -v "\\*|release|master|develop|feature/xxx" | awk '{ print substr($0, 10)}' | xargs -I branch git push origin :branch && git fetch --prune

git branch -r --merged
merge済のbranchリストを取得

egrep -v "\\*|release|master|develop|feature/xxx"
merge済であっても消して欲しくないbranchを指定

消して欲しくないremotebranchについてはしっかりチームメンバーに聞きましょうね!
アクシデントの元になる恐れがあります:scream:

awk '{ print substr($0, 10)}'
egrepの出力結果ではorigin/が文字列に含まれており後に控えるremotebranchを消すコマンドで都合が悪いため文字をoridin/develop -> developとなるように整形

xargs -I branch git push origin :branch
整形された文字列を引数としてremotebranchを消す

git fetch --prune
remoteで削除されたbranchをlocalに反映

実行前にどのremotebranchが削除対象なのか確認

git branch -r --merged | egrep -v "\\*|release|master|develop|feature/xxx" | awk '{ print substr($0, 10)}' | xargs -I branch echo branch

最後に

remotebranchを消す方法については何個もやり方はあると思いますが、
一例としてみてもらえると幸いです:relaxed:

2
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
2
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?