FETCH_HEAD引用在“git fetch”之后没有正确更新。 [英] FETCH_HEAD reference not updating correctly after "git fetch"

查看:1454
本文介绍了FETCH_HEAD引用在“git fetch”之后没有正确更新。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从远端存储的本地存储库。运行 git pull 以及 git fetch; git merge FETCH_HEAD 用于执行完全相同的操作,正如预期的那样描述 git pull


描述



将远程存储库中的更改合并到当前分支中。在默认模式下,git pull是git fetch的缩写,然后是git merge FETCH_HEAD。


目前,意外的是运行 git fetch 正确停止更新 FETCH_HEAD 引用。 FETCH_HEAD 现在被固定到一个旧的提交。运行 git fetch 将所有更改下载到远程跟踪分支,但FETCH_HEAD保持不变,无论其运行分支如何。

 #目前在branchone中
> git fetch

#branchone是最新的,因为...
> git rev-parse branchone
593539e8a98ba5980d4b645db3b0f506bb9b6a2c

#...它在与远程分支相同的提交中
> git rev-parse origin / branchone
593539e8a98ba5980d4b645db3b0f506bb9b6a2c

#但是FETCH_HEAD显示不同的内容
> git rev-parse FETCH_HEAD
37301df96597ac037f8e7e846fea6fc7df77bea5

git pull 仍然执行正确的任务。但是运行 git fetch; git merge FETCH_HEAD 会做一些不同的事情,因为 FETCH_HEAD 指向一个不正确的提交。



是否有任何设置或问题可能与 git fetch 行为有关?

解决方案

在没有任何选项的情况下运行 git fetch 会获取远程中的所有引用,并将它们写入 .git / FETCH_HEAD 文件。文件通常的内容如下所示:

  37301df96597ac037f8e7e846fea6fc7df77bea5 github.com:user/repo $ b的分支'master' $ b 593539e8a98ba5980d4b645db3b0f506bb9b6a2c github.com:user/repo 
的非合并分支'branchOne'

当在 .git 目录下有这样的文件,只要该文件中的第一个内容是40个字符的十六进制数就可以用作参考,或者一个较短的十六进制数,与现有的提交匹配。

; cat .git / MAGIC_HEAD
其他并不重要
冰箱

#因此它会被许多git命令解释,像这样
> git rev-parse MAGIC_HEAD
deadbeefdeadbeefdeadbeefdeadbeefdeadbeef

了解这一点后,我们可以看到,在运行 git fetch 引用 FETCH_HEAD 将解析为第一行中发生的任何事情

 #假设已经提到的.git / FETCH_HEAD 
> git rev-parse FETCH_HEAD
37301df96597ac037f8e7e846fea6fc7df77bea5

看起来像<$ c的内容顺序$ c> .git / FETCH_HEAD 并不保证首先包含当前分支的引用。



通过在不同的存储库中尝试它,似乎在某些第一行总是当前分支,因此 git fetch; git merge FETCH_HEAD 按预期工作。但是,在其他存储库中, .git / FETCH_HEAD 的内容将按不同的顺序排列,通常第一行将是对不同分支的远程提交的引用, code> FETCH_HEAD 引用不正确。



为什么它的行为不同对我来说是一个谜。



作为解决方案,如果使用 git fetch remote_name branch_name ,则只会提取此特定分支,并且只有该单行将出现在 .git / FETCH_HEAD ,使得 FETCH_HEAD 引用始终正确。

 #只会抓取分支
> git fetch origin branchone

#FETCH_HEAD将只包含一行
> cat .git / FETCH_HEAD
593539e8a98ba5980d4b645db3b0f506bb9b6a2c github.com:user/repo
的分支'branchOne'


I have a local repository that pulls from a remote one. Running git pull as well as git fetch; git merge FETCH_HEAD used to perform exactly the same action, as is expected from the description of git pull:

DESCRIPTION

Incorporates changes from a remote repository into the current branch. In its default mode, git pull is shorthand for git fetch followed by git merge FETCH_HEAD.

Presently, and unexpectedly, running git fetch stopped updating the FETCH_HEAD reference correctly. FETCH_HEAD is now stuck to an old commit. Running git fetch downloads all changes to remote tracked branches, but FETCH_HEAD remains unchanged regardless of the branch in which it is run.

# currently in branchone
> git fetch

# branchone is up to date since...
> git rev-parse branchone
593539e8a98ba5980d4b645db3b0f506bb9b6a2c

# ...its in the same commit as the remote branch
> git rev-parse origin/branchone
593539e8a98ba5980d4b645db3b0f506bb9b6a2c

# however FETCH_HEAD shows something different
> git rev-parse FETCH_HEAD
37301df96597ac037f8e7e846fea6fc7df77bea5

git pull still performs the correct task. However running git fetch; git merge FETCH_HEAD will do something different since FETCH_HEAD points to an incorrect commit.

Is there any setting or issue that could be messing with git fetch behavior?

解决方案

Running git fetch without any options will fetch all references in the remotes and write them to the .git/FETCH_HEAD file. The contents of the file usualy looks something like this:

37301df96597ac037f8e7e846fea6fc7df77bea5 branch 'master' of github.com:user/repo
593539e8a98ba5980d4b645db3b0f506bb9b6a2c not-for-merge branch 'branchOne' of github.com:user/repo

When you have a file like this under the .git directory, you can use it as a reference as long as the first thing in that file is either a 40 character hex number, or a shorter hex number that actualy matches to an existing commit.

# 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

Knowing this we can see that after running git fetch the reference FETCH_HEAD will resolve to is whatever happens to be in that first line

# Assuming the already mentioned contents of .git/FETCH_HEAD
> git rev-parse FETCH_HEAD
37301df96597ac037f8e7e846fea6fc7df77bea5

Seems like the order of the contents of .git/FETCH_HEAD is not guaranteed to contain first the reference for the current branch.

By trying it in different repositories it seems that in some the first line is always the current branch, and thus git fetch; git merge FETCH_HEAD works as expected. On other repositories however the contents of .git/FETCH_HEAD will be ordered differently and often the first line will be a reference to the remote commit of a different branch, thus making the FETCH_HEAD reference incorrect.

Why it behaves differently is a mystery to me.

As a solution, if git fetch remote_name branch_name is used only this specific branch is fetched and only that single line will appear in the contents of .git/FETCH_HEAD, making the FETCH_HEAD reference always correct.

# 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

这篇关于FETCH_HEAD引用在“git fetch”之后没有正确更新。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆