git 如何删除所有已经集成的远程git分支?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8229737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 06:11:28  来源:igfitidea点击:

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

gitgit-branch

提问by ThiefMaster

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

在工作中,我们使用主题分支,这些分支在某些时候集成到几个 (3) 主分支中。现在我想从我的远程存储库中删除所有已完全集成到主分支的主题分支。如果这是不可能的,也可以检索已集成的本地分支列表。

回答by Michael Krelin - hacker

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 2- | xargs -n 20 git push --delete origin

Explanation:

解释:

  • git branch -r --merged origin/master
    • -r/--remoteslist the remote-tracking branches.
    • --merged origin/masteronly list branches whose tips are reachable from origin/master.
  • grep -v masterremove any branch name containing masterfrom list.1-vmeans negative match.
  • grep "origin/"select only branches on originremote.
  • cut -d "/" -f 2-drop the origin/prefix
  • xargs -n 20 git push --delete origindo something similar to git push --delete origin branch-a branch-b branch-c …
    • -n 20/--max-args=20use at most 20 arguments per command line.
  • git branch -r --merged origin/master
    • -r/--remotes列出远程跟踪分支。
    • --merged origin/master仅列出其提示可从origin/master.
  • grep -v mastermaster从列表中删除包含的任何分支名称。1-v表示否定匹配。
  • grep "origin/"仅选择origin远程分支。
  • cut -d "/" -f 2-去掉origin/前缀
  • xargs -n 20 git push --delete origin做类似的事情 git push --delete origin branch-a branch-b branch-c …
    • -n 20/--max-args=20每个命令行最多使用 20 个参数。

As for -n, I choose 20 just as an example. Fewer arguments will make it slower, for example -n 1makes 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 200will 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).

至于-n,我选择 20 作为例子。更少的参数会使它变慢,例如-n 1让它一次删除一个;你有更多的进度提示,因为它会在每次删除一个分支时报告。更多的参数-n 200会使它更快(更少的总时间),但它每 200 个分支只报告一次,让你认为它一开始被冻结(其实不是)。根据您的需要调整数字。如果省略此选项,则默认数字非常大(在我的机器中为 2048)。

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

1. 请注意,这也会删除origin/HEAD -> origin/master,但origin/HEAD无论如何您都不想弄乱。

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

给出一个分支列表,将是与 git branch --merged master

回答by Adam Dymitruk

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.

如果您发现经常这样做,请将其转储到名为“clean”的脚本中。

回答by Nox73

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

回答by Arturo Herrero

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

这些是我用来删除所有合并到origin/master. 基本上,我删除了master从 GitHub合并到的所有分支。

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

回答by Bashir Momen

Just for Powershell and windows users.

仅适用于 Powershell 和 Windows 用户。

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