Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does FETCH_HEAD in Git mean?

git pull --help says:

In its default mode, git pull is shorthand for git fetch followed by git merge FETCH_HEAD.

What is this FETCH_HEAD and what is actually merged during git pull?

like image 894
Misha Moroshko Avatar asked Feb 11 '12 02:02

Misha Moroshko


People also ask

Can I delete Fetch_head?

2 answers. Yes you can delete FETCH_HEAD files. They are only temporary.

What is git pull origin master?

git pull origin master will pull changes from the origin remote, master branch and merge them to the local checked-out branch. git pull origin/master will pull changes from the locally stored branch origin/master and merge that to the local checked-out branch.

What git pull rebase do?

Git pull rebase is a method of combining your local unpublished changes with the latest published changes on your remote. Let's say you have a local copy of your project's main branch with unpublished changes, and that branch is one commit behind the origin/main branch.

How do I Refetch in git?

When you do a git fetch, it fetches all the changes from the remote repository and stores it in a separate branch in your local repository. You can reflect those changes in your corresponding branches by merging. So basically, git pull = git fetch + git merge.


2 Answers

FETCH_HEAD is a short-lived ref, to keep track of what has just been fetched from the remote repository. git pull first invokes git fetch, in normal cases fetching a branch from the remote; FETCH_HEAD points to the tip of this branch (it stores the SHA1 of the commit, just as branches do). git pull then invokes git merge, merging FETCH_HEAD into the current branch.

The result is exactly what you'd expect: the commit at the tip of the appropriate remote branch is merged into the commit at the tip of your current branch.

This is a bit like doing git fetch without arguments (or git remote update), updating all your remote branches, then running git merge origin/<branch>, but using FETCH_HEAD internally instead to refer to whatever single ref was fetched, instead of needing to name things.

like image 74
Cascabel Avatar answered Oct 06 '22 06:10

Cascabel


The FETCH_HEAD is a reference to the tip of the last fetch, whether that fetch was initiated directly using the fetch command or as part of a pull. The current value of FETCH_HEAD is stored in the .git folder in a file named, you guessed it, FETCH_HEAD.

So if I issue:

git fetch https://github.com/ryanmaxwell/Fragaria 

FETCH_HEAD may contain

3cfda7cfdcf9fb78b44d991f8470df56723658d3        https://github.com/ryanmaxwell/Fragaria 

If I have the remote repo configured as a remote tracking branch then I can follow my fetch with a merge of the tracking branch. If I don't I can merge the tip of the last fetch directly using FETCH_HEAD.

git merge FETCH_HEAD 
like image 33
Jonathan Mitchell Avatar answered Oct 06 '22 07:10

Jonathan Mitchell