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

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

git branch -D 3.2.*

并杀死所有3.2。x分支。

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


当前回答

Use

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

其他回答

博士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.

如果你在windows上,你可以使用powershell来做这个:

 git branch | grep 'feature/*' |% { git branch -D $_.trim() }

用你喜欢的图案替换feature/*。

git branch  | cut -c3- | egrep "^3.2" | xargs git branch -D
  ^                ^                ^         ^ 
  |                |                |         |--- create arguments
  |                |                |              from standard input
  |                |                |
  |                |                |---your regexp 
  |                |
  |                |--- skip asterisk 
  |--- list all 
       local
       branches

编辑:

一个更安全的版本(由Jakub narabbski和Jefromi建议),因为git分支输出并不用于脚本:

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

... 或者xargs-free:

git branch -D `git for-each-ref --format="%(refname:short)" refs/heads/3.2\*`

最近,我在寻找同样问题的解决方案,最后我找到了一个答案,它工作得很好:

打开终端,或等价的。 输入git分支| grep " pattern "预览分支 这将被删除。 输入git branch | grep " pattern " | xargs git branch -D

这个解决方案非常棒,如果你想要每个命令的完整解释以及它是如何工作的,可以在这里给出。

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