[英]git branch -r shows different remote shared repo in different local working directory
I created a shared repo and then clone it into two folders (A and B). 我创建了一个共享仓库,然后将其克隆到两个文件夹(A和B)中。 All in the same PC.
所有都在同一台电脑上。
Now there are two branches, master
and v0
. 现在有两个分支,
master
和v0
。 Folder A and B are now in branch v0
. 文件夹A和B现在位于分支
v0
。 On AI deleted the v0
branch on remote repo. 在AI上删除了远程仓库上的
v0
分支。
$ git push origin --delete v0
To file:///home/nanger/github/shared1.git
- [deleted] v0
and then I tried to pull the remote repo on both A and B (in branch v0
): 然后我尝试在A和B上拉动远程仓库(在分支
v0
):
Now, I see that 现在,我明白了
on A: 在A:
$ git pull
Already up-to-date.
$ git branch -r
origin/master
$ git branch
* master
v0
on B: 在B上:
$ git pull
Already up-to-date
$ git branch -r
origin/HEAD -> origin/master
origin/master
origin/v0
$ git branch
* master
v0
Why do A and B have different views of the remote repo? 为什么A和B对远程仓库有不同的看法?
git pull
doesn't remove local remote-tracking branches, such as origin/v0
, that are tracking remote-branches that no longer exist on the remote. git pull
不会删除跟踪远程不再存在的远程分支的本地远程跟踪分支,例如origin/v0
。
If you want to remove obsolete remote-tracking branches from your local repo, you need to use 如果要从本地存储库中删除过时的远程跟踪分支,则需要使用
git fetch <remote> --prune
# Or shorter
git fetch <remote> -p
From Jan's mirror of the official git fetch
docs : 从Jan的镜像官方
git fetch
docs :
-p --prune
After fetching, remove any remote-tracking references that no longer exist on the remote.
获取后,删除远程不再存在的任何远程跟踪引用。 Tags are not subject to pruning if they are fetched only because of the default tag auto-following or due to a
--tags
option.如果仅由于默认标记自动跟踪或由于
--tags
选项而提取标记,则不对其进行修剪。 However, if tags are fetched due to an explicit refspec (either on the command line or in the remote configuration, for example if the remote was cloned with the--mirror
option), then they are also subject to pruning.但是,如果由于显式refspec(在命令行或远程配置中,例如,如果使用
--mirror
选项克隆远程)而--mirror
,则它们也会受到修剪。
If you want an even more detailed explanation about the different types of branches in Git (local, remote, and remote-tracking), and how to delete them, you can read about them all in my answer to How do I delete a Git branch both locally and remotely? 如果你想要更详细地解释Git中不同类型的分支(本地,远程和远程跟踪),以及如何删除它们,你可以在我如何删除Git分支的答案中阅读所有这些内容。 本地和远程? .
。
Why would you expect them to be the same? 你为什么期望它们是一样的? (This is meant as a real question; I can think of some reasons to expect that. Obviously they're not the same, so those reasons must be wrong. You may come up with a different set of "possible reasons to expect them to be the same" than I would, though, so it's a useful exercise for you to think about why you expected them to be the same.)
(这是一个真正的问题;我可以想出一些理由来预期。显然它们不一样,所以这些理由一定是错的。你可能会想出一套不同的“可能的理由来期待它们但是,“我会这样做,所以这是一个有用的练习,让你思考为什么你期望它们是相同的。”
In any case, git doesn't really care very much about what's in any other repository (on the same computer, or a different computer). 在任何情况下,git并不真正关心任何其他存储库(在同一台计算机或不同的计算机上)中的内容。 It works on one repository at a time and cares only about what's in that one repository.
它一次只能在一个存储库上运行,并且只关心该存储库中的内容。
It's also important to note here that git pull
is a script that does two things: first, it runs git fetch
, which is the actual command that contacts some other git (usually, but not necessarily , on some other computer) to find out "what they have that you don't". 这里还要注意
git pull
是一个执行两件事的脚本:首先,它运行git fetch
,这是与其他git(通常但不一定在某些其他计算机上)联系的实际命令,以找出“他们有什么,你没有“。 Then, after that's done, git pull
runs either git merge
or git rebase
1 if needed. 然后,在完成之后,如果需要,
git pull
运行git merge
或git rebase
1 。
The "other git" is called "the remote", and for a typical clone, there's just one remote, origin
. “其他git”被称为“远程”,对于典型的克隆,只有一个远程
origin
。
The git push
command also talks to a (or the) "remote"—which, again, is usually, but not necessarily, on a different computer—and asks that the remote, which is another git repository, update its (the remote's) local branches. git push
命令还与(或)“远程”对话 - 它通常(但不一定)在不同的计算机上 - 并且要求远程(另一个git存储库)更新它(远程的)当地分公司。
We now need one more bit of background information. 我们现在需要一点背景信息。 Your own git tries to keep track of "what's on the remote" using "remote-tracking" branches.
你自己的git尝试使用“远程跟踪”分支跟踪“遥控器上有什么”。 These are the branches shown as
origin/ whatever
. 这些分支显示为
origin/ whatever
。 Again, this is independent of where the remote is: these copies of the remote-branch labels are just stored locally. 同样,这与远程控制的位置无关:远程分支标签的这些副本仅存储在本地。
When you did git push --delete v0
you had your git call up the other git and ask it (the other git) to delete its v0
. 当你执行
git push --delete v0
你让你的git调用另一个git并要求它(另一个git)删除它的v0
。 It did—it deleted the (local) v0
branch that it had before then—and your git then deleted your local copy, that your git called origin/v0
. 它确实删除了它之前的(本地)
v0
分支 - 然后你的git删除了你的本地副本,你的git称为origin/v0
。
When you later changed to a different clone, you ran git pull
which ran git fetch
. 当你以后改为另一个克隆时,你运行了
git pull
,它运行了git fetch
。 This git fetch
calls up the other git (usually on another computer, but in this case actually on the same laptop). 这个
git fetch
调用了另一个git(通常是在另一台计算机上,但在这种情况下实际上是在同一台笔记本电脑上)。 I'm going to gloss over some details here (details that depend on the particular git version: there was a fairly major change in git 1.8.4 or so) and pretend you ran git fetch
(which avoids needing to care about these details, that don't actually affect the final outcome). 我将在这里略过一些细节(细节取决于特定的git版本:git 1.8.4左右有一个相当大的变化)并假装你运行
git fetch
(这避免了需要关心这些细节,这实际上并不影响最终结果)。
This time, your git fetch
calls up the remote git and asks it what it has now, and the remote does not list v0
, since it's now gone from the remote. 这一次,你的
git fetch
调用远程git并询问它现在有什么,而遥控器没有列出v0
,因为它现在已经从遥控器中消失了。 This gives your (local) git a little bit of a problem. 这给你的(本地)git带来了一些问题。 Your local git got
v0
from that remote git some time earlier, and now it's gone. 你的本地git早些时候从那个远程git获得了
v0
,现在它已经消失了。 Should your local git delete its origin/v0
, or should it keep it? 你的本地git应该删除它的
origin/v0
,还是应该保留它?
The answer that the git programmers chose here is that your git should keep its origin/v0
around, in case you were using it for something. git程序员在这里选择的答案是你的git应该保持其
origin/v0
,以防你使用它的东西。 If you want your git fetch
to delete your local origin/v0
copy of the v0
that's now missing on the remote, you should add the --prune
option to your git fetch
command. 如果您希望
git fetch
删除遥控器上现在缺少的v0
本地origin/v0
副本,则应该在git fetch
命令中添加--prune
选项。 2 This tells your local git to remove your local origin/ whatever
whenever the remote's whatever
has gone away. 2这告诉你当地的git删除本地
origin/ whatever
,只要遥控器上的whatever
已经消失。 Otherwise, your local git keeps it. 否则,你当地的git会保留它。
Why does git push --delete
delete it, when git fetch
without --prune
keeps it? 为什么
git push --delete
删除它,当git fetch
没有--prune
保留它? That's a question for the people writing git itself. 对于编写git本身的人来说,这是一个问题。 3
3
1 The default is to run git merge
, if you don't set up anything to override the default. 1默认情况下,如果您没有设置任何内容来覆盖默认值,则运行
git merge
。 Since git rebase
is probably a better default, there are a lot of ways to make pull
use rebase
. 由于
git rebase
可能是一个更好的默认情况下,有很多的方式,使pull
使用rebase
。
2 You can also use git remote prune origin
or git remote update origin --prune
. 2您也可以使用
git remote prune origin
或git remote update origin --prune
。 There were some minor git bugs that made each one act slightly differently. 有一些小的git错误使每个人的行为略有不同。 As far as I know they're all fixed in git versions 2.0 or higher.
据我所知,他们都固定在git版本2.0或更高版本。
3 I can guess , 4 of course: it seems that if you, the user running git push
, are saying you want to delete a branch from a remote, you probably don't particularly want to keep your local copy of their branch once you've successfully deleted their branch. 3我猜 , 4当然:似乎如果你,运行
git push
的用户,你说要从远程删除分支,你可能不会特别想保留你的分支的本地副本一旦你已经成功删除了他们的分支。 But it is just a guess. 但这只是猜测。
4 I can call spirits from the vasty deep too, but they have never come forth. 4 我也可以从邪恶的深处召唤灵魂 ,但他们从未出现过。 :-)
:-)
Depending on your configuration, git pull may pull only the branch you are on. 根据您的配置, git pull可能只会拉动您所在的分支。 Try git fetch, and then git branch -r.
尝试git fetch,然后git branch -r。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.