[英]Git prune did not delete my local branches
I deleted a bunch of merged branches on Github.com, I did a git pull, and all my branches are still showing up.我在 Github.com 上删除了一堆合并的分支,我做了一个 git pull,我所有的分支仍然出现。
Hence, I ran..因此,我跑了..
git remote prune origin
It showed that the branches were pruned in the output.它表明分支在输出中被修剪。
However, all my deleted branches show up when I run但是,当我运行时,我所有已删除的分支都会出现
git branch
I assume I did something different then I expected?我假设我做了一些与预期不同的事情? What did I do?
我做了什么? How do I delete my branches from local that are already deleted from GitHub?
如何从本地删除已从 GitHub 中删除的分支?
In this post: git remote prune origin does not delete the local branch even if its upstream remote branch is deleted在这篇文章中: git remote prune origin 不会删除本地分支,即使其上游远程分支被删除
It mentioned the same issue but I do not understand they mean, when they say it deletes stale remote branches from local.它提到了同样的问题,但我不明白他们的意思,当他们说它从本地删除陈旧的远程分支时。
It's not supposed to delete them.不应该删除它们。
Your local branches—which really should just be called "your branches"—are yours .你当地的分支机构——实际上应该被称为“你的分支机构”——是你的。 Only your remote-tracking names ,
origin/*
in this case, are not yours.在这种情况下,只有您的远程跟踪名称
origin/*
不是您的。 1 So if origin
had a branch named xyzzy
earlier, but doesn't any more, git fetch --prune origin
or git remote prune origin
deletes your origin/xyzzy
to match origin
's lack-of- xyzzy
. 1因此,如果
origin
之前有一个名为xyzzy
的分支,但不再存在, git fetch --prune origin
或git remote prune origin
会删除您的origin/xyzzy
以匹配origin
的缺失xyzzy
。
It doesn't touch your branches, which are yours .它不会触及你的树枝,这是你的。 If you are sure that they can go now, you'll have to delete them yourself.
如果您确定它们现在可以删除,则必须自己删除它们。 People have written scripts to do this, but said scripts tend to be dumb: they will often delete your
xyzzy
even if you have commits you forgot to push before mergiing, or meant to move to another branch , and once your xyzzy
is deleted, it's often too late.人们已经编写了脚本来执行此操作,但是说脚本往往很愚蠢:即使您在合并之前忘记推送或打算移动到另一个分支的提交,他们也会经常删除您的
xyzzy
,并且一旦您的xyzzy
被删除,它就是往往为时已晚。 So be careful with such scripts.所以要小心这样的脚本。
(It might be nice to have an option to these commands that tells them: If I have a local branch xyzzy
that points to the same commit as origin/xyzzy
, and you're deleting origin/xyzzy
, delete my local xyzzy
too. Moreover, if I have xyzzy
, but origin/xyzzy
does not match my xyzzy
, complain and stop, so that I can figure out what to do about this. But they do not have such an option.) (如果我有一个本地分支
xyzzy
指向与origin/xyzzy
相同的提交,并且您要删除origin/xyzzy
,那么也可以删除我的本地xyzzy
。 ,如果我有xyzzy
,但origin/xyzzy
与我的xyzzy
不匹配,请抱怨并停止,以便我可以弄清楚该怎么做。但他们没有这样的选择。)
1 Technically, these are yours, too. 1从技术上讲,这些也是你的。 It's just that your Git is set up so that when your Git calls up their Git—the one at
origin
—and gets branch names and hash IDs from them, your Git overwrites your own origin/master
with their updated master
.只是您的 Git 被设置为当您的 Git 调用它们的 Git(
origin
的 Git)并从它们获取分支名称和哈希 ID 时,您的 Git 会用更新的master
覆盖您自己的origin/master
master
。 So while origin/master
is a name in your Git repository's "name-to-hash-ID" database, it's not a name you would normally control yourself.因此,虽然
origin/master
是您的 Git 存储库的“name-to-hash-ID”数据库中的一个名称,但它通常不是您自己控制的名称。 You just let your Git mirror their Git, with their master
becoming your origin/master
.你只需让你的 Git 镜像他们的 Git,他们的
master
成为你的origin/master
。
Or, to say it shorter: your Git's origin/master
is your Git's way of remembering their Git's master
.或者,简而言之:您的 Git 的
origin/master
是您的 Git 记住他们的 Git 的master
。 So it's sort-of theirs.所以这是他们的。
As torek said already, the first key point is to prune your remote-tracking branches to make sure you're comparing against a fresh list of references.正如torek已经说过的,第一个关键点是修剪您的远程跟踪分支,以确保您正在与新的参考列表进行比较。 You can use
您可以使用
git fetch --prune
then spot the local "leftover" branches with :然后发现当地的“剩余”分支:
git for-each-ref --format='%(if)%(upstream:short)%(then)%(else)%(color:bold red)%(end)%(refname:short)' refs/heads
(they'll appear in red in the output) (它们会在输出中显示为红色)
or alternatively (assuming your remote is named origin
) :或者(假设您的遥控器名为
origin
):
git branch -vv | grep -v origin/
...and the last phase is to manually delete them with ...最后一个阶段是手动删除它们
git branch -d <branch>
Since you have them displayed just handy, you should not suffer more than a couple of copy/pasting.由于您将它们显示得非常方便,因此您不应该遭受超过几次复制/粘贴的痛苦。 If the number of stale branches is regularly massive or, let's say, important enough, then maybe something in the workflow could be put in place to avoid the recurring situation?
如果陈旧分支的数量经常很大,或者说,足够重要,那么也许可以在工作流程中放置一些东西来避免重复发生的情况?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.