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

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

git branch -D 3.2.*

并杀死所有3.2。x分支。

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


当前回答

保持“开发”,并删除所有其他在本地

    git branch | grep -v "develop" | xargs git branch -D 

其他回答

如果你想删除多个分支进行清理,这是可行的

 git branch -d branch1 branch2 branch3

此外,如果你想反映删除操作到远程,你可以使用这个来推送它们

 git push origin --delete branch1 branch2 branch3

我把我的首字母和破折号(at-)作为分支名称的前三个字符,原因如下:

git branch -D `git branch --list 'at-*'`

博士TL;

git branch -D $(git branch | grep '3\.2\. *')

解释

git branch lists all the branches on your local system. grep '3\.2\..*' uses pattern matching to find all files in the current working directory starting with 3.2.. Using \ to escape . as it's a special character for grep. git branch | grep '3\.2\..*' will pass all the github branch names to the grep command which will then look for branch names starting with the string within the list supplied. $(git branch | grep '3\.2\..*') Anything enclosed within $() will run it as a separate shell command whose result can then be passed on to a separate command. In our case, we would want the list of files found to be deleted. git branch -D $(git branch | grep '3\.2\..*') This just does what is explained above in Point 4.

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

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

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

git branch | grep "<pattern>"

在这种情况下

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

对于使用PowerShell的纯灵魂,这里有一个小脚本 Git branch -d $(Git branch——list '3.2.;*' | %{$_.Trim()})