简体   繁体   English

git branch -r在不同的本地工作目录中显示不同的远程共享仓库

[英]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 . 现在有两个分支, masterv0 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

Documentation 文档

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 mergegit 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 origingit 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. 34当然:似乎如果你,运行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.

相关问题 将远程分支拉入具有不同名称的本地仓库? - Pull remote branch into local repo with different name? Git-将本地分支移动到其他远程分支 - Git - Moving a local branch to different remote branch git-svn如何合并从本地git repo到其他远程svn分支的更改? - git-svn How can I merge changes from a local git repo to a different remote svn branch? GIT远程和本地仓库上的不同文件 - GIT different files on remote and local repo Git:使用分支及其远程在不同分支上修复存储库 - Git: Fix repo with a branch and its remote on different branches Git:在本地和远程存储库中保留一个不同的目录吗? - Git: keep a directory different in local and remote repository? 从本地分支,git push到另一个远程分支 - From a local branch, git push to a different remote branch 如何用git覆盖不同本地分支的远程分支 - How to overwrite remote branch with different local branch with git 如何签出远程 git 分支? 或者将远程分支与不同的 git 目录分支链接? - How to checkout a remote git branch ? Or to link a remote branch with a different git directory branch? 如何在本地工作目录,远程工作目录和git ftp目标中拥有不同版本的文件? - How can I have different versions of a file in the local working directory, remote working directory and git ftp target?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM