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

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

git branch -D 3.2.*

并杀死所有3.2。x分支。

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


当前回答

嗯,在最坏的情况下,你可以用:

git branch | grep '3\.2' | xargs git branch -D

其他回答

不是那种语法。但你可以这样做:

git branch -D 3.2 3.2.1 3.2.2

基本上,git分支会通过一次调用为你删除多个分支。不幸的是,它不做分支名称补全。不过,在bash中,你可以这样做:

git branch -D `git branch | grep -E '^3\.2\..*'`

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

 git branch -d branch1 branch2 branch3

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

 git push origin --delete branch1 branch2 branch3
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 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分支-d $

它将删除所有本地合并分支,除了您当前签入的分支。

这是一个让你的地方变得干净的好方法