How to delete all remote git branches which have already been integrated?

陌路散爱 提交于 2019-12-03 01:58:38

问题


At work we are using topic branches which are integrated into a few (3) master branches at some point. Now I'd like to delete all topic branches from my remote repository which have been fully integrated into a master branch. If that's not possible, retrieving a list of local branches which have been integrated would be fine, too.


回答1:


Another answer edited in by someone who thought it's the best (and it looks good):

git branch -r --merged origin/master | grep -v master | grep "origin/" | cut -d "/" -f 3- | xargs -n 20 git push --delete origin

Explanation:

  • git branch -r --merged origin/master
    • -r/--remotes list the remote-tracking branches.
    • --merged origin/master only list branches whose tips are reachable from origin/master.
  • grep -v master remove any branch name containing master from list.1-v means negative match.
  • grep "origin/" select only branches on origin remote.
  • cut -d "/" -f 2- drop the origin/ prefix
  • xargs -n 20 git push --delete origin do something similar to git push --delete origin branch-a branch-b branch-c …
    • -n 20/--max-args=20 use at most 20 arguments per command line.

As for -n, I choose 20 just as an example. Fewer arguments will make it slower, for example -n 1 makes it delete one at a time; you have more progress hints, because it will report each time it deletes a branch. More arguments like -n 200 will make it faster (less total time), but it only reports once every 200 branches, making you think that it is frozen at first (while it is not). Tune the number to your need. If you omit this option, the default number is really large (2048 in my machine).

1. Note that this also removes origin/HEAD -> origin/master, but you won't want to mess with origin/HEAD anyway.

Original answer:

git push --delete remote topicbranch

or

git push remote :topicbranch

Giving a list of branches, would be something with git branch --merged master




回答2:


You can do this in one go with

git branch --merged master | grep -v master | xargs -n 1 git push --delete origin

Dump that in a script called 'clean' if you find you're doing this often.




回答3:


If you want to delete remote branches from origin repository:

git branch -r --merged develop | egrep -iv '(master|develop)' | sed 's/origin\///g' | xargs -n 1 git push --delete origin



回答4:


These are the commands I'm using to remove everything merge into origin/master. Basically, I remove all the branches merged into master from GitHub.

git remote update -p &&
git branch -r --merged origin/master |
grep origin |
grep -v master |
cut -d"/" -f2- |
xargs git push origin --delete



回答5:


Just for Powershell and windows users.

    git branch -r --merged  | findstr /v "origin/master" | %{git push origin --delete $_.Trim().Substring(7)}


来源:https://stackoverflow.com/questions/8229737/how-to-delete-all-remote-git-branches-which-have-already-been-integrated

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!