What is HEAD in Git?

Tim Mouskhelichvili
Tim Mouskhelichvili
• 3 minutes to read

If you have been using Git long enough, you have seen references or occurrences of HEAD in Git commands on online tutorials, videos, or documentation. But what is it exactly, and what does it do?

In Git, HEAD points to the latest commit of the active branch.

This article will explain, in detail, HEAD in Git, its notation, and explain the detached HEAD situation.

Let's get to it 😎.

git head

What is HEAD in Git?

HEAD refers to the last commit of the active branch.

By default, HEAD points to the latest commit of the main branch (also called the master branch).

To view the commit that HEAD points to, use the git show command like so:

bashgit show HEAD

This command will output all the information about the latest commit of the current branch.

What is HEAD~ (with a tilde)?

When you use a tilde with HEAD, you can go back a number of generations in the history.

For example:

HEAD~ or HEAD~1 refers to the first commit before the latest commit.

HEAD~2 refers to the second commit before the latest commit.

HEAD~3 refers to the third commit before the latest commit.

And so on.

What is HEAD^ (with a caret)?

When you use a caret with HEAD, you can go down to immediate parents of the commit.

This notation is rarely used and only used on merge commits.

What is HEAD@{} (with an at-sign)?

When you use an at-sign with HEAD, it refers to the position of the commit in your local reflog.

For example:

HEAD@{2} refers to the second commit in your reflog.

The .git/HEAD file

The current branch referenced by HEAD is stored inside a file at .git/HEAD.

To view this file, use the cat command:

bash> cat .git/HEAD
ref: refs/heads/main

The last part of the output corresponds to the active branch, in this case, the main branch.

When you change the active branch using the git checkout command, the content of that file also changes.

bash> cat .git/HEAD
ref: refs/heads/feature-1

In this case, the HEAD points to another branch called feature-1.

Note: If this file gets corrupted, you will get a "fatal: not a git repository" error. I've written an extensive guide on how to fix it.

HEAD vs head in Git?

HEAD vs head

The most significant difference between those two terms is:

  • head in lowercase refers to any commit at the tip of a Git branch.
  • HEAD in uppercase refers to the latest commit of the active branch.

What is a detached HEAD in Git?

A detached HEAD in Git means that HEAD does not point to the latest commit of a branch.

It means that you are on a single commit.

How to fix a detached HEAD?

To fix a detached HEAD, checkout to the tip of your active branch.

If you want to DELETE the changes from the detached HEAD:

1. Checkout to your main branch using the git checkout command.

bashgit checkout main

If you want to SAVE the changes from the detached HEAD:

1. Create a new temporary branch.

bashgit branch tmp

2. Checkout to your main branch using the git checkout command.

bashgit checkout main

3. Merge the temporary branch to your main branch.

bashgit merge tmp

Final thoughts

As you can see, HEAD is a relatively easy concept to learn but a crucial one to know about to use Git to its fullest potential.

git head

Here are some other Git tutorials for you to enjoy:

Comments (2)
Reply to:
Katie Morrow May 27, 2023 21:06PM
Pretty! This has been a really wonderful post. Many thanks for providing these details.
REPLY
Mylie Wiggins May 25, 2023 22:04PM
I’m often to blogging and i really appreciate your content. The article has actually peaks my interest. I’m going to bookmark your web site and maintain checking for brand spanking new information.
REPLY