如何修剪远程不存在的本地跟踪分支 [英] How to prune local tracking branches that do not exist on remote anymore
问题描述
使用 git remote prune origin
我可以删除远程没有的本地分支。
但我想删除那些从这些远程分支创建的本地分支(检查它们是否合并会很好)。
我该怎么做?
修剪完成后,您可以使用 git branch -r
。可以使用 git branch -vv
检索具有远程跟踪分支的分支机构列表。因此,使用这两个列表,您可以找到不在远程列表中的远程跟踪分支。
这一行应该做到这一点(需要 bash
或 zsh
,将不能用于标准的Bourne shell):
git branch -r | awk'{print $ 1}'| egrep -v -f / dev / fd / 0 <(git branch -vv | grep origin)| awk'{print $ 1}'| xargs git branch -d
该字符串获取远程分支的列表并将它传递给 egrep
通过标准输入。并过滤具有远程跟踪分支的分支(使用 git branch -vv
并过滤那些具有 origin
的分支)然后获取该输出的第一列,这将是分支名称。最后将所有分支名称传递到delete分支命令中。
因为它使用 -d
选项,不会删除当您运行此命令时尚未合并到您所在分支中的分支。
还要记住,您需要运行 git fetch --prune
first ,否则 git branch -r
仍会看到远程分支。 p>
With git remote prune origin
I can remove the local branches that are not any more on the remote.
But I want to remove also those local branches that were created from those remote branches (a check if they are unmerged would be nice).
How can I do this?
After pruning, you can get the list of remote branches with git branch -r
. The list of branches with their remote tracking branch can be retrieved with git branch -vv
. So using these two lists you can find the remote tracking branches that are not in the list of remotes.
This line should do the trick (requires bash
or zsh
, won't work with standard Bourne shell):
git branch -r | awk '{print $1}' | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk '{print $1}' | xargs git branch -d
This string gets the list of remote branches and passes it into egrep
through the standard input. And filters the branches that have a remote tracking branch (using git branch -vv
and filtering for those that have origin
) then getting the first column of that output which will be the branch name. Finally passing all the branch names into the delete branch command.
Since it is using the -d
option, it will not delete branches that have not been merged into the branch that you are on when you run this command.
Also remember that you'll need to run git fetch --prune
first, otherwise git branch -r
will still see the remote branches.
这篇关于如何修剪远程不存在的本地跟踪分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!