What is HEAD in Git?

You see the Git documentation saying things like

The branch must be fully merged in HEAD.

But what is Git HEAD exactly?


You can think of the HEAD as the "current branch". When you switch branches with git checkout , the HEAD revision changes to point to the tip of the new branch.

You can see what HEAD points to by doing:

cat .git/HEAD

In my case, the output is:

$ cat .git/HEAD
ref: refs/heads/master

It is possible for HEAD to refer to a specific revision that is not associated with a branch name. This situation is called a detached HEAD.


To quote other people:

A head is simply a reference to a commit object. Each head has a name (branch name or tag name, etc). By default, there is a head in every repository called master. A repository can contain any number of heads. At any given time, one head is selected as the “current head.” This head is aliased to HEAD, always in capitals".

Note this difference: a “head” (lowercase) refers to any one of the named heads in the repository; “HEAD” (uppercase) refers exclusively to the currently active head. This distinction is used frequently in Git documentation.

Another good source that quickly covers the inner workings of git (and therefor a better understanding of heads/HEAD) can be found here. References (ref:) or heads or branches can be considered like post-it notes stuck onto commits in the commit history. Usually they point to the tip of series of commits, but they can be moved around with git checkout or git revert etc.


I recommend this definition from github developer Scott Chacon [video reference]:

Head is your current branch. It is a symbolic reference. It is a reference to a branch. You always have HEAD, but HEAD will be pointing to one of these other pointers, to one of the branches that you're on. It is the parent of your next commit. It is what should be what was last checked-out into your working directory... This is the last known state of what your working directory was.

The whole video will give a fair introduction to the whole git system so I also recommend you to watch it all if have the time to.

链接地址: http://www.djcxy.com/p/36234.html

上一篇: Git中HEAD,工作树和索引之间有什么区别?

下一篇: Git中的HEAD是什么?