我想清理我的本地存储库,它有大量的旧分支:例如3.2、3.2.1、3.2.2等。

我希望有个鬼鬼祟祟的办法能一次性把他们干掉。因为它们大多遵循点释放约定,我想也许有一个捷径可以说:

git branch -D 3.2.*

并杀死所有3.2。x分支。

我尝试了这个命令,当然,它不起作用。


当前回答

你可以删除所有的分支,删除所有不必要的引用:

rm . /文献/头/ 3.2 . *

其他回答

Powershell的解决方案:

如果你正在运行窗口,你可以使用PowerShell一次删除多个分支…

git branch -D ((git branch | Select-String -Pattern '^\s*3\.2\..*') | foreach{$_.ToString().Trim()})
//this will remove all branches starting with 3.2.

git branch -D ((git branch | Select-String -Pattern 'feature-*') | foreach{$_.ToString().Trim()})
// this will delete all branches that contains the word "feature-" as a substring.

您还可以使用Powershell的Select-String命令对模式匹配的工作方式进行微调。看一下powershell文档。

git branch -D <branchName>

这里有一个一般的解决方案:

git branch | grep "<pattern>" | xargs git branch -D

我建议在执行上述命令之前运行以下命令行,预览将要删除的分支。

git branch | grep "<pattern>"

在这种情况下

git branch | grep "^3\.2\..*" | xargs git branch -D

你可以使用git gui一次删除多个分支。 从命令提示符/Bash -> git gui ->远程->删除分支…—>选择需要移除的远程分支—>删除。

Use

git for-each-ref --format='%(refname:short)' 'refs/heads/3.2.*' |
   xargs git branch -D