我想清理我的本地存储库,它有大量的旧分支:例如3.2、3.2.1、3.2.2等。
我希望有个鬼鬼祟祟的办法能一次性把他们干掉。因为它们大多遵循点释放约定,我想也许有一个捷径可以说:
git branch -D 3.2.*
并杀死所有3.2。x分支。
我尝试了这个命令,当然,它不起作用。
我想清理我的本地存储库,它有大量的旧分支:例如3.2、3.2.1、3.2.2等。
我希望有个鬼鬼祟祟的办法能一次性把他们干掉。因为它们大多遵循点释放约定,我想也许有一个捷径可以说:
git branch -D 3.2.*
并杀死所有3.2。x分支。
我尝试了这个命令,当然,它不起作用。
当前回答
git branch -d branch1 branch2 branch3已经可以工作了,但是在git 2.31(2021年第一季度)中会更快。 以前,当删除许多分支和标记时,代码习惯一次引用一个。 还有另一个API可以用来删除多个裁判,当裁判被打包时,它会产生相当大的性能差异。
参见菲尔·霍德提交的8198907(2021年1月20日)。 (由Junio C Hamano—gitster—在commit f6ef8ba中合并,2021年2月5日)
8198907795:删除标签或分支时使用delete_refs 致谢:以利亚·纽伦 署名:菲尔·霍德
'git tag -d'(man) accepts one or more tag refs to delete, but each deletion is done by calling delete_ref on each argv. This is very slow when removing from packed refs. Use delete_refs instead so all the removals can be done inside a single transaction with a single update. Do the same for 'git branch -d'(man). Since delete_refs performs all the packed-refs delete operations inside a single transaction, if any of the deletes fail then all them will be skipped. In practice, none of them should fail since we verify the hash of each one before calling delete_refs, but some network error or odd permissions problem could have different results after this change. Also, since the file-backed deletions are not performed in the same transaction, those could succeed even when the packed-refs transaction fails. After deleting branches, remove the branch config only if the branch ref was removed and was not subsequently added back in. A manual test deleting 24,000 tags took about 30 minutes using delete_ref. It takes about 5 seconds using delete_refs.
其他回答
你可以使用git gui一次删除多个分支。 从命令提示符/Bash -> git gui ->远程->删除分支…—>选择需要移除的远程分支—>删除。
要根据指定的模式删除多个分支,请执行以下操作:
打开终端或同等工具,输入以下命令:
git branch | grep "<pattern>" (preview of the branches based on pattern)
git branch | grep "<pattern>" | xargs git branch -D (replace the <pattern> with a regular expression to match your branch names)
删除所有3.2。X个分支,你需要输入
git branch | grep "3.2" | xargs git branch -D
这是所有!
你可以开始了!
使用以下命令删除所有分支(签出的分支不会被删除)。
git branch | cut -d '*' -f 1 | tr -d " \t\r" | xargs git branch -d
编辑:我用的是Mac Os
最近,我在寻找同样问题的解决方案,最后我找到了一个答案,它工作得很好:
打开终端,或等价的。 输入git分支| grep " pattern "预览分支 这将被删除。 输入git branch | grep " pattern " | xargs git branch -D
这个解决方案非常棒,如果你想要每个命令的完整解释以及它是如何工作的,可以在这里给出。
我的具体情况没有解决,所以我重新回答…
场景:
新克隆的回购 未创建本地分支 许多远程分支(在我的例子中超过100个) 我想从远程删除所有的特征分支,feature/myfeature
这是我在工作中得到的命令:
git branch -a | cut -c3- | grep 'origin\/feature' | sed 's/remotes\/origin\///' | xargs git push origin --delete
感谢@gwai让我走上正轨。