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

标签 git branch git-fetch

这主要是出于好奇,因为我正在尝试熟悉 Git。我查看了“git fetch”的文档,但没有看到对以下内容的明显解释。提前致谢,如果这非常明显,我们深表歉意。

1) 从一个中央存储库,比如 GitHub,我在两台机器 HostAHostB 上分别克隆了一个名为 website 的存储库。

2) 在 HostA 上,我更改了一个文件,例如 README.txt,然后提交。
此时在 HostA 上,分支 masterorigin/master 正如预期的那样不同,因为我还没有推送

git show master
git show origin/master

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

3) 一旦我推送,他们在那之后是一样的。


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

git fetch
git merge FETCH_HEAD

之后,在 HostB 上 masterorigin/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ębskianswer )
    换句话说,您没有指定 refspec 的目的地

关于git fetch 与 git fetch origin master 对跟踪分支有不同的影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11892517/

相关文章:

tfs - TFS 2010 中的许多分支有哪些可扩展性和性能限制?

version-control - 在 Perforce 中更改分支

Git svn fetch、rebase、reset 和 dcommit 函数在一个 svn 分支上全部挂起且没有错误

git - 使用 gitlab 自定义接收后 Hook

git:定期推送重新基址分支的更好选择

git - 如何获取/pull 与通配符字符串匹配的多个分支?

git - git fetch 命令中的 --append 选项有什么用?

git - 使用Git从 "Repo"仓库同步一个项目

git - 如何公开使用子模块,但符号链接(symbolic link)到本地​​的单个克隆?

git - Docker 从 git repo 运行命令?