Git: What's HEAD

By Xah Lee. Date: . Last updated: .

What is HEAD

HEAD refers to a specific commit ID. It usually reference to the last commit of current branch in your local repository.

.git/HEAD
A file. The content is a Git: Commit ID. This commit id is what HEAD refers to.

Here's detail:

In other words, think of “heads” as variables, and their values are specific commit id. And HEAD is a specific variable, whose value is the “current” commit id.

How to find what commit is current HEAD

Look in the file .git/HEAD.

here's the content of my file:

ref: refs/heads/master

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.

How to Set HEAD

You can use git-reset to set HEAD.

See man git-reset.

What is HEAD~

HEAD~ is a syntax.

Here's what it means.

(when there are more than 1 parent, the second etc are ignored. It simply go up the ancestry via 1st parent.)

What is HEAD^

HEAD^ is a syntax.

When current branch's commits never had any 2nd parent, that is, no merge ever happened, then, HEAD^ and HEAD~ means the same thing.

Else, each occurrence of ^ refers to the 2nd parent, when there's one, or 3rd parent if currently on 2nd parent, etc, and if none, it refers to just parent up wards.

git FAQ