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

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

git branch -D 3.2.*

并杀死所有3.2。x分支。

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


当前回答

因为在Git中,所有的分支都没有引用到Git repo,为什么不删除指向. Git /ref的分支,然后如果有任何在存储库中不感兴趣的东西被遗漏了,就会自动被垃圾收集,所以你不需要麻烦。

其他回答

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

博士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 GUI(这是Windows的默认附加组件),那么它是最简单的。可以使用“ctrl”键选择多个分支,一键删除。

要根据指定的模式删除多个分支,请执行以下操作:

打开终端或同等工具,输入以下命令:

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

这是所有!

你可以开始了!

如果你正在使用Fish shell,你可以利用字符串函数:

git branch -d (git branch -l "<your pattern>" | string trim)

这与其他一些答案中的Powershell选项没有太大区别。