git fetch 与 git fetch origin master 对跟踪分支有不同的影响

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11892517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 14:21:41  来源:igfitidea点击:

git fetch vs. git fetch origin master have different effects on tracking branch

gitbranchgit-fetch

提问by Xoanon93

This is mostly of the nature of a curiosity as I'm trying to get familiar with Git. I have looked at the documentation for 'git fetch' but I don't see an obvious explanation for the below. Thanks in advance, and apologies if this is howlingly obvious.

这主要是好奇心的性质,因为我试图熟悉 Git。我已经查看了 'git fetch' 的文档,但我没有看到以下内容的明显解释。提前致谢,如果这太明显了,我深表歉意。

1) From a central repository, say GitHub, I clone a repository named websiteon each of two machines, HostAand HostB.

1) 从一个中央存储库,比如说 GitHub,我克隆了website一个在两台机器上命名的存储库,HostA并且HostB.

2) on HostA, I make a change to a file, say README.txt, and commit it.
At this point on HostA, the commits for branches masterand origin/masterare, as expected different since I haven't pushed yet

2) 在 上HostA,我对文件进行了更改,例如README.txt,并提交了它。
在这一点上HostA,分支的提交masterorigin/master预期不同,因为我还没有推送

git show master
git show origin/master

report different hashes (since masterhas the change and origin/masterdoes not)

报告不同的哈希值(因为master有变化而origin/master没有)

3) Once I push, they are after that the same.

3)一旦我推动,他们之后是一样的。



4) Now, over on HostB, if I do the following:

4)现在,结束HostB,如果我执行以下操作:

git fetch
git merge FETCH_HEAD

afterwards, on HostB masterand origin/masterreport the same hash when queried with git show

之后,在 HostB 上,masterorigin/master在查询时报告相同的哈希值git show

BUT

if instead I had done, on HostB:

如果相反我做了,在HostB

git fetch origin master
git merge FETCH_HEAD

at that point the hashes still differ.

那时哈希值仍然不同。

git show origin
git show origin/master

report differenthashes

报告不同的哈希值

The tracking branch origin/masterisn't updated until I do a plain git fetch

跟踪分支origin/master直到我做一个简单的git fetch

Why is this?

为什么是这样?

回答by VonC

If your branch has an associated remote tracking branchthat means its configuration is like:

如果您的分支具有关联的远程跟踪分支,则意味着其配置如下:

git config branch.[branch-name].remote [remote-name]
git config branch.[branch-name].merge [remote-master]

The key part of git fetchwhich explain the difference between the two commands is:

git fetch其中解释两个命令区别的关键部分是:

<refspec>

The format of a <refspec>parameter is an optional plus +, followed by the source ref <src>, followed by a colon :, followed by the destination ref <dst>.
The remote ref that matches <src>is fetched, and if <dst>is not empty string, the local ref that matches it is fast-forwarded using <src>.

<refspec>参数的格式是一个可选的加号+,后跟源引用<src>,后跟冒号:,后跟目标引用<dst>。获取
匹配的远程 ref,如果不是空字符串,则匹配它的本地 ref 使用.<src><dst><src>

Let me repeat it:

让我重复一遍:

if <dst>is not empty string, the local ref that matches it is fast-forwarded using <src>.
Knowing that:

如果<dst>不是空字符串,匹配它的本地 ref 使用<src>.
知道:

  • git fetchis equivalent to git fetch origin master:master(from the default value of your branch config), so it will update the remote tracking branch: the destination of the refspec is specified for you.

  • git fetch origin masteris equivalent to "git fetch origin master:", not to "git fetch origin master:master"; it stores fetched value of 'master' branch (of remote 'origin') in FETCH_HEAD, and not in 'master' branch or remote-tracking 'remotes/origin/master' branch (from Jakub Nar?bski's answer)
    In other words, you didn't specify the destination of your refspec

  • git fetch相当于git fetch origin master:master(从你的分支配置的默认值),所以它会更新远程跟踪分支:为你指定了 refspec 的目的地

  • git fetch origin master相当于“ git fetch origin master:”,而不是“ git fetch origin master:master”;它将 ' master' 分支(远程 ' origin')的提取值存储在 中FETCH_HEAD,而不是在 ' master' 分支或远程跟踪 ' remotes/origin/master' 分支中(来自Jakub Nar?bski回答
    换句话说,您没有指定目的地你的refspec

回答by Ethan Brown

The answer lies in the messages you get back from git fetch. In the first case, when you fetch without providing a refspec, you'll see that the remote tracking branches are updated:

答案在于您收到的消息git fetch。在第一种情况下,当您在不提供 refspec 的情况下进行提取时,您将看到远程跟踪分支已更新:

remote: Counting objects: 5, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From /depot
   c67d1c8..1941673  master     -> origin/master

Note how the message says that origin/master is updated with the master from the origin.

请注意该消息如何说明 origin/master 已使用来自 origin 的 master 进行更新。

Now in the second case, where you specify the refspec, you get something altogether different:

现在在第二种情况下,您指定 refspec,您会得到完全不同的东西:

remote: Counting objects: 5, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From /depot
 * branch            master     -> FETCH_HEAD

So when you specify the refspec, the remote tracking branch (origin/master) is NOT updated, only FETCH_HEAD.

因此,当您指定 refspec 时,远程跟踪分支(origin/master)不会更新,只会更新 FETCH_HEAD。

The end result is that you'll appear to be ahead of origin/master when you're not really. I can't imagine why this behavior would be desirable, but it's definitely an interesting little quirk of the fetch command.

最终的结果是,当你不是真的时,你看起来会领先于原点/大师。我无法想象为什么这种行为是可取的,但这绝对是 fetch 命令的一个有趣的小怪癖。

回答by UpAndAdam

If you want to fast forward merge yourself, or use git pull. You don't seem to understand that the purpose of git fetch is NOT to update your working tree. Fetch is meant to update your tracking branches.

如果你想自己快进合并,或者使用 git pull。您似乎不明白 git fetch 的目的不是更新您的工作树。Fetch 旨在更新您的跟踪分支。