这主要是出于好奇,因为我正在尝试熟悉 Git。我查看了“git fetch”的文档,但没有看到对以下内容的明显解释。提前致谢,如果这非常明显,我们深表歉意。
1) 从一个中央存储库,比如 GitHub,我在两台机器 HostA
和 HostB
上分别克隆了一个名为 website
的存储库。
2) 在 HostA
上,我更改了一个文件,例如 README.txt
,然后提交。
此时在 HostA
上,分支 master
和
origin/master
正如预期的那样不同,因为我还没有推送
git show master
git show origin/master
报告不同的哈希值(因为master
有变化而origin/master
没有)
3) 一旦我推送,他们在那之后是一样的。
4) 现在,在 HostB
上,如果我执行以下操作:
git fetch
git merge FETCH_HEAD
之后,在 HostB 上 master
和 origin/master
在使用 git show
查询时报告相同的哈希值
但是
如果相反,我在 HostB
上完成了:
git fetch origin master
git merge FETCH_HEAD
此时哈希仍然不同。
git show origin
git show origin/master
报告不同的哈希值
跟踪分支 origin/master
在我执行普通 git fetch
之前不会更新
这是为什么?
最佳答案
如果您的分支机构有关联的 remote tracking branch这意味着它的配置是这样的:
git config branch.[branch-name].remote [remote-name]
git config branch.[branch-name].merge [remote-master]
的关键部分 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>
.
再说一遍:
如果<dst>
不是空字符串,匹配它的本地引用使用 <src>
快进.
知道:
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 的 answer )
换句话说,您没有指定 refspec 的目的地
关于git fetch 与 git fetch origin master 对跟踪分支有不同的影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11892517/