FETCH_HEAD link not updating correctly after "git fetch"

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

DESCRIPTION

Includes changes from the remote repository to the current branch. In default mode, git pull is short for git fetch followed by git merge FETCH_HEAD.

Currently and unexpectedly, git fetch stopped correctly updating the FETCH_HEAD link. FETCH_HEAD now bound to the old commit. Running git fetch uploads all changes to the remote tracked branches, but FETCH_HEAD remains the same regardless of the branch in which it is running.

 # 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 is doing the right thing. However, running git fetch; git merge FETCH_HEAD git fetch; git merge FETCH_HEAD will do something else, since FETCH_HEAD indicates an incorrect commit.

Are there any tweaks or issues that might be messy with git fetch behavior?

+7
source share
3 answers

Running git fetch without any parameters will extract all the links to the remotes and write them to a .git/FETCH_HEAD . Typically, the contents of a file look 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 such a file in the .git directory, you can use it as a link if the first thing in this file is either a 40-character hexadecimal number or a shorter hexadecimal number, 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 see that after running git fetch FETCH_HEAD link will be resolved to what happens on the first line

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

It seems that the order of the contents of .git/FETCH_HEAD not guaranteed to contain the first link for the current branch.

Having tried it in different repositories, it seems that in the first line there is always the current branch, and thus git fetch; git merge FETCH_HEAD git fetch; git merge FETCH_HEAD works as expected. However, in other repositories, the contents of .git/FETCH_HEAD will be ordered differently, and often the first line will be a link to the remote commit of another branch, which makes the FETCH_HEAD link FETCH_HEAD .

Why this behaves differently is a mystery to me.

As a solution, if git fetch remote_name branch_name , only this particular branch is selected, and only that one line will be displayed in the contents of .git/FETCH_HEAD , making the FETCH_HEAD link always valid.

 # 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 
+7
source

Just try to get your head to point to the last commit / click that was made.

Use this in the GIT repository:

 git reset --hard HEAD@ {1} 

Hoping this can solve your problem by taking it at a time when it works just fine, as before.

0
source

After running git fetch (without arguments), FETCH_HEAD will contain a link valid for merging (i.e., not marked as not for merging) if the current local branch (i.e. HEAD ) is a tracking branch.

The solution is to either make the current branch a tracking branch (see How to make an existing Git tracking branch a remote branch? ) Or specify the remote branch and the branch to retrieve (i.e. git fetch origin branch

0
source

All Articles