Running git fetch
不带任何选项将获取遥控器中的所有引用并将它们写入.git/FETCH_HEAD
文件。文件的内容通常如下所示:
37301df96597ac037f8e7e846fea6fc7df77bea5 branch 'master' of github.com:user/repo
593539e8a98ba5980d4b645db3b0f506bb9b6a2c not-for-merge branch 'branchOne' of github.com:user/repo
当你在下面有一个这样的文件时.git
目录中,您可以将其用作参考,只要该文件中的第一个内容是 40 个字符的十六进制数字,或者是实际与现有提交匹配的较短的十六进制数字。
# This file can be used as a reference
> cat .git/MAGIC_HEAD
deadbeefdeadbeefdeadbeefdeadbeefdeadbeef lorem ipsum
the rest does not really matter
refrigerator
# And thus it will be interpreted by many git commands like this
> git rev-parse MAGIC_HEAD
deadbeefdeadbeefdeadbeefdeadbeefdeadbeef
知道了这一点我们可以看到运行后git fetch
参考资料FETCH_HEAD
将解决第一行中发生的任何事情
# Assuming the already mentioned contents of .git/FETCH_HEAD
> git rev-parse FETCH_HEAD
37301df96597ac037f8e7e846fea6fc7df77bea5
看起来像内容的顺序.git/FETCH_HEAD
不保证首先包含当前分支的引用。
通过在不同的存储库中尝试它,似乎在某些存储库中第一行始终是当前分支,因此git fetch; git merge FETCH_HEAD
按预期工作。然而在其他存储库上的内容.git/FETCH_HEAD
的顺序会有所不同,并且第一行通常是对不同分支的远程提交的引用,从而使得FETCH_HEAD
参考不正确。
为什么它的行为不同对我来说是个谜。
作为一个解决方案,如果git fetch remote_name branch_name
仅使用此特定分支,并且只有该行会出现在内容中.git/FETCH_HEAD
,使得FETCH_HEAD
参考始终正确。
# Will only fetch branchone
> git fetch origin branchone
# FETCH_HEAD will contain only a single line
> cat .git/FETCH_HEAD
593539e8a98ba5980d4b645db3b0f506bb9b6a2c branch 'branchOne' of github.com:user/repo