如何修剪远程不再存在的本地跟踪分支

本文翻译自:How to prune local tracking branches that do not exist on remote anymore

With git remote prune origin I can remove the local branches that are not on the remote any more. 使用git remote prune origin我可以删除不在远程上的本地分支。

But I also want to remove local branches that were created from those remote branches (a check if they are unmerged would be nice). 但是我还想删除从那些远程分支创建的本地分支(检查它们是否未合并将是不错的选择)。

How can I do this? 我怎样才能做到这一点?


#1楼

参考:https://stackoom.com/question/sohZ/如何修剪远程不再存在的本地跟踪分支


#2楼

not sure how to do it all at once, but git git branch -d <branchname> will delete a local branch ONLY if it is completely merged. 不确定如何一次完成所有操作,但是git git branch -d <branchname>仅在完全合并git branch -d <branchname>删除本地分支。 Note the lowercase d. 注意小写字母d。

git branch -D <branchname> (note the capital D) will delete a local branch regardless of its merged status. git branch -D <branchname> (注意大写D)将删除本地分支,无论其合并状态如何。


#3楼

If you want to delete all local branches that are already merged into master, you can use the following command: 如果要删除已经合并到master中的所有本地分支,可以使用以下命令:

git branch --merged master | grep -v '^[ *]*master$' | xargs git branch -d

More info . 更多信息


#4楼

Amidst the information presented by git help fetch , there is this little item: git help fetch提供的信息中,有一个小项目:

 -p, --prune
        After fetching, remove any remote-tracking branches which no longer exist on the remote.

So, perhaps, git fetch -p is what you are looking for? 那么,也许您正在寻找git fetch -p

EDIT: Ok, for those still debating this answer 3 years after the fact, here's a little more information on why I presented this answer... 编辑:好的,对于那些在事实发生三年后仍在争论这个答案的人,这里有更多关于我为什么提出这个答案的信息。

First, the OP says they want to "remove also those local branches that were created from those remote branches [that are not any more on the remote]". 首先,OP表示他们希望“也删除从那些远程分支[不再在远程]上创建的本地分支”。 This is not unambiguously possible in git . git这是不可能做到的。 Here's an example. 这是一个例子。

Let's say I have a repo on a central server, and it has two branches, called A and B . 假设我在中央服务器上有一个存储库,它有两个分支,称为AB If I clone that repo to my local system, my clone will have local refs (not actual branches yet) called origin/A and origin/B . 如果我将该存储库克隆到本地系统,则我的克隆将具有称为refs origin/Aorigin/B本地引用(尚未是实际的分支)。 Now let's say I do the following: 现在说我要执行以下操作:

git checkout -b A origin/A
git checkout -b Z origin/B
git checkout -b C <some hash>

The pertinent facts here are that I for some reason chose to create a branch on my local repo that has a different name than its origin, and I also have a local branch that does not (yet) exist on the origin repo. 这里的相关事实是,由于某种原因,我选择了在本地存储库中创建一个名称与原始名称不同的分支,并且我也有一个本地分支(尚未)存在于原始存储库上。

Now let's say I remove both the A and B branches on the remote repo and update my local repo ( git fetch of some form), which causes my local refs origin/A and origin/B to disappear. 现在,假设我删除了远程仓库中的AB分支,并更新了本地仓库(某种形式的git fetch ),这导致我的本地引用origin/Aorigin/B消失了。 Now, my local repo has three branches still, A , Z , and C . 现在,我的本地仓库仍然有三个分支AZC None of these have a corresponding branch on the remote repo. 这些都没有在远程仓库上有相应的分支。 Two of them were "created from ... remote branches", but even if I know that there used to be a branch called B on the origin, I have no way to know that Z was created from B , because it was renamed in the process, probably for a good reason. 其中两个是“从...远程分支创建的”,但是即使我知道在原点上曾经有一个名为B的分支,我也无法知道Z是从B创建的,因为Z被重命名为这个过程,可能有充分的理由。 So, really, without some external process recording branch origin metadata, or a human who knows the history, it is impossible to tell which of the three branches, if any, the OP is targeting for removal. 因此,实际上,如果没有任何外部过程来记录分支机构的元数据,或者没有一个知道历史的人员,就无法确定OP的目标是删除这三个分支中的哪个(如果有)。 Without some external information that git does not automatically maintain for you, git fetch -p is about as close as you can get, and any automatic method for literally attempting what the OP asked runs the risk of either deleting too many branches, or missing some that the OP would otherwise want deleted. 没有一些git无法自动为您维护的外部信息, git fetch -p会尽可能地接近您,并且任何试图按OP要求自动执行的自动方法都可能会删除太多分支或丢失一些分支否则OP将要删除。

There are other scenarios, as well, such as if I create three separate branches off origin/A to test three different approaches to something, and then origin/A goes away. 同样,还有其他情况,例如,如果我在origin/A创建了三个单独的分支来测试某种事物的三种不同方法,然后origin/A消失了。 Now I have three branches, which obviously can't all match name-wise, but they were created from origin/A , and so a literal interpretation of the OPs question would require removing all three. 现在,我有三个分支,显然不能都按名称匹配,但是它们是从origin/A创建origin/A ,因此对OP问题的字面解释将要求删除所有三个。 However, that may not be desirable, if you could even find a reliable way to match them... 但是,如果您甚至可以找到一种可靠的方式来匹配它们,那可能就不那么理想了……


#5楼

After pruning, you can get the list of remote branches with git branch -r . 修剪后,您可以使用git branch -r获得远程分支的列表。 The list of branches with their remote tracking branch can be retrieved with git branch -vv . 带有远程跟踪分支的分支列表可以使用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): 这行代码可以解决问题(需要bashzsh ,不适用于标准的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. 该字符串获取远程分支的列表,并将其通过标准输入传递到egrep 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. 并过滤具有远程跟踪分支的分支(使用git branch -vv并对具有origin git branch -vv过滤),然后获取该输出的第一列,即分支名称。 Finally passing all the branch names into the delete branch command. 最后,将所有分支名称传递给delete分支命令。

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. 由于它使用-d选项,因此它不会删除尚未合并到运行该命令时所在的分支的分支。

Also remember that you'll need to run git fetch --prune first , otherwise git branch -r will still see the remote branches. 还记得你需要运行git fetch --prune 第一 ,否则git branch -r仍然会看到远程分支。


#6楼

I'm pretty sure that git remote prune origin is what you want. 我很确定git remote prune origin是您想要的。

You can run it as git remote prune origin --dry-run to see what it would do without making any changes. 您可以运行它作为git remote prune origin --dry-run ,看看它不作任何更改做。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值