Git: What's HEAD

By Xah Lee. Date: . Last updated: .

What is HEAD?

HEAD (all cap) refers to a specific commit. You can see which it is by looking at the file .git/HEAD

Here's detail:

Here's quote from git help glossary:


A named reference to the commit at the tip of a branch. Heads are stored in a file in $GIT_DIR/refs/heads/ directory, except when using packed refs. (See git-pack-refs(1).)


The current branch. In more detail: Your working tree is normally derived from the state of the tree referred to by HEAD. HEAD is a reference to one of the heads in your repository, except when using a detached HEAD, in which case it directly references an arbitrary commit.

How to find what commit is current HEAD?

Look in the file .git/HEAD.

You can do so by the command

# show the commit id the current HEAD points to
cd my_project_dir
cat .git/HEAD

Sample output:

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

The refs/heads/master is again a reference. It is a file, you can look at .git/refs/heads/master

The content on my disk is: 7cc94381a53ef1e20bfe8a5529ab1aaa616b0696

What is detached HEAD?

When HEAD is pointing to a commit ID directly, instead of a named branch, it's called detached HEAD.

Here's quote from git help glossary:

detached HEAD

Normally the HEAD stores the name of a branch, and commands that operate on the history HEAD represents operate on the history leading to the tip of the branch the HEAD points at. However, Git also allows you to check out an arbitrary commit that isn’t necessarily the tip of any particular branch. The HEAD in such a state is called “detached”.

Note that commands that operate on the history of the current branch (e.g. git commit to build a new history on top of it) still work while the HEAD is detached. They update the HEAD to point at the tip of the updated history without affecting any branch. Commands that update or inquire information about the current branch (e.g. git branch --set-upstream-to that sets what remote-tracking branch the current branch integrates with) obviously do not work, as there is no (real) current branch to ask about in this state.