我想清理我的本地存储库,它有大量的旧分支:例如3.2、3.2.1、3.2.2等。
我希望有个鬼鬼祟祟的办法能一次性把他们干掉。因为它们大多遵循点释放约定,我想也许有一个捷径可以说:
git branch -D 3.2.*
并杀死所有3.2。x分支。
我尝试了这个命令,当然,它不起作用。
我想清理我的本地存储库,它有大量的旧分支:例如3.2、3.2.1、3.2.2等。
我希望有个鬼鬼祟祟的办法能一次性把他们干掉。因为它们大多遵循点释放约定,我想也许有一个捷径可以说:
git branch -D 3.2.*
并杀死所有3.2。x分支。
我尝试了这个命令,当然,它不起作用。
不是那种语法。但你可以这样做:
git branch -D 3.2 3.2.1 3.2.2
基本上,git分支会通过一次调用为你删除多个分支。不幸的是,它不做分支名称补全。不过,在bash中,你可以这样做:
git branch -D `git branch | grep -E '^3\.2\..*'`
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\*`
Use
git for-each-ref --format='%(refname:short)' 'refs/heads/3.2.*' |
xargs git branch -D
因为在Git中,所有的分支都没有引用到Git repo,为什么不删除指向. Git /ref的分支,然后如果有任何在存储库中不感兴趣的东西被遗漏了,就会自动被垃圾收集,所以你不需要麻烦。
基于@gawi(上面)提供的答案,我做了一个可能有用的小函数。
removeBranchesWithPrefix() {
git for-each-ref --format="%(refname:short)" refs/heads/$1\* | xargs git branch -d
}
将其添加到.bash_profile并重新启动终端。然后你可以像这样从命令行调用:
removeBranchesWithPrefix somePrefix
Note
我有它目前设置为软删除,这意味着它不会删除分支,除非他们已经合并。如果你喜欢在边缘上生活,将-d更改为-d,它将删除所有带有前缀的东西!
你可能会喜欢这个小物件…在最终删除所有选择之前,它会拉出列表并要求确认每个项目……
git branch -d `git for-each-ref --format="%(refname:short)" refs/heads/\* | while read -r line; do read -p "remove branch: $line (y/N)?" answer </dev/tty; case "$answer" in y|Y) echo "$line";; esac; done`
使用-D强制删除(像往常一样)。
为了可读性,这里是逐行分解的…
git branch -d `git for-each-ref --format="%(refname:short)" refs/heads/\* |
while read -r line; do
read -p "remove branch: $line (y/N)?" answer </dev/tty;
case "$answer" in y|Y) echo "$line";;
esac;
done`
下面是xargs方法……
git for-each-ref --format="%(refname:short)" refs/heads/\* |
while read -r line; do
read -p "remove branch: $line (y/N)?" answer </dev/tty;
case "$answer" in
y|Y) echo "$line";;
esac;
done | xargs git branch -D
最后,我喜欢把它放在。bashrc中
alias gitselect='git for-each-ref --format="%(refname:short)" refs/heads/\* | while read -r line; do read -p "select branch: $line (y/N)?" answer </dev/tty; case "$answer" in y|Y) echo "$line";; esac; done'
这样我就可以说
gitSelect | xargs git branch -D.
如果你在windows上,你可以使用powershell来做这个:
git branch | grep 'feature/*' |% { git branch -D $_.trim() }
用你喜欢的图案替换feature/*。
最近,我在寻找同样问题的解决方案,最后我找到了一个答案,它工作得很好:
打开终端,或等价的。 输入git分支| grep " pattern "预览分支 这将被删除。 输入git branch | grep " pattern " | xargs git branch -D
这个解决方案非常棒,如果你想要每个命令的完整解释以及它是如何工作的,可以在这里给出。
你可以使用git branch——list列出符合条件的分支,并使用git branch -D/ -D删除符合条件的分支。
一个程序示例:
git branch -d `git branch --list '3.2.*'`
对于使用PowerShell的纯灵魂,这里有一个小脚本 Git branch -d $(Git branch——list '3.2.;*' | %{$_.Trim()})
也许你会发现这很有用:
如果你想删除所有不是'master', 'foo'和'bar'的分支
git branch -D `git branch | grep -vE 'master|foo|bar'`
Grep -v 'something'是一个带有反转的匹配器。
这对我来说是正确的:
git branch | xargs git branch -d
git branch | xargs git branch -D
删除所有本地分支
使用以下命令删除所有分支(签出的分支不会被删除)。
git branch | cut -d '*' -f 1 | tr -d " \t\r" | xargs git branch -d
编辑:我用的是Mac Os
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文档。
我今天刚刚清理了一大批过时的本地/远程分支机构。
以下是我的做法:
1. 列出所有分支名称到一个文本文件:
git branch -a >> BranchNames.txt
2. 附加我想删除的分支到命令"git branch -D -r":
git branch -D -r origin/dirA/branch01 origin/dirB/branch02 origin/dirC/branch03(…附加任何分支名称)
3.执行CMD命令
这一个比“git push origin -delete”工作得更快更可靠。
这可能不是其他答案中列出的最聪明的方法,但这个方法非常直接,而且易于使用。
如果你正在使用Fish shell,你可以利用字符串函数:
git branch -d (git branch -l "<your pattern>" | string trim)
这与其他一些答案中的Powershell选项没有太大区别。
如果你要在一个文本文件(branches-to-del.txt)中删除所有的分支(每行一个分支),那么你可以这样做,从远程(origin)中删除所有这样的分支: Xargs -一个分支到del.txt git推送-删除原点
要根据指定的模式删除多个分支,请执行以下操作:
打开终端或同等工具,输入以下命令:
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
这是所有!
你可以开始了!
如果你想删除多个分支进行清理,这是可行的
git branch -d branch1 branch2 branch3
此外,如果你想反映删除操作到远程,你可以使用这个来推送它们
git push origin --delete branch1 branch2 branch3
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 branch | grep "<pattern>" | xargs git branch -D
我建议在执行上述命令之前运行以下命令行,预览将要删除的分支。
git branch | grep "<pattern>"
在这种情况下
git branch | grep "^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.
我的具体情况没有解决,所以我重新回答…
场景:
新克隆的回购 未创建本地分支 许多远程分支(在我的例子中超过100个) 我想从远程删除所有的特征分支,feature/myfeature
这是我在工作中得到的命令:
git branch -a | cut -c3- | grep 'origin\/feature' | sed 's/remotes\/origin\///' | xargs git push origin --delete
感谢@gwai让我走上正轨。