我想清理我的本地存储库,它有大量的旧分支:例如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分支。
我尝试了这个命令,当然,它不起作用。
当前回答
基于@gawi(上面)提供的答案,我做了一个可能有用的小函数。
removeBranchesWithPrefix() {
git for-each-ref --format="%(refname:short)" refs/heads/$1\* | xargs git branch -d
}
将其添加到.bash_profile并重新启动终端。然后你可以像这样从命令行调用:
removeBranchesWithPrefix somePrefix
Note
我有它目前设置为软删除,这意味着它不会删除分支,除非他们已经合并。如果你喜欢在边缘上生活,将-d更改为-d,它将删除所有带有前缀的东西!
其他回答
因为在Git中,所有的分支都没有引用到Git repo,为什么不删除指向. Git /ref的分支,然后如果有任何在存储库中不感兴趣的东西被遗漏了,就会自动被垃圾收集,所以你不需要麻烦。
如果你要在一个文本文件(branches-to-del.txt)中删除所有的分支(每行一个分支),那么你可以这样做,从远程(origin)中删除所有这样的分支: Xargs -一个分支到del.txt git推送-删除原点
你可能会喜欢这个小物件…在最终删除所有选择之前,它会拉出列表并要求确认每个项目……
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.
博士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.
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文档。