Git HEAD Explained (HEAD~ vs HEAD^ vs HEAD@{} with Examples)

Git HEAD Explained (HEAD~ vs HEAD^ vs HEAD@{} with Examples)

Git HEAD is one of the most important concepts in Git that helps you understand your current position in the commit history, especially when dealing with detached HEAD explained. Many users get confused with HEAD~, HEAD^, and HEAD@{}, but these references are essential for navigating commits efficiently. In this guide, you will learn what HEAD means in Git and how to use tilde (~) and caret (^) with practical examples.


Git HEAD, ~, ^ and @{} Cheat Sheet

ReferenceMeaningExampleUse Case
HEADCurrent commitgit show HEADView latest commit
HEAD^ or HEAD^1First parent of current commitgit show HEAD^View previous commit
HEAD^2Second parent (merge commit)git show HEAD^2Inspect merged branch
HEAD~1Same as HEAD^git show HEAD~1Move 1 commit back
HEAD~2Two commits before HEADgit log HEAD~2Navigate history
HEAD~nN commits before HEADgit diff HEAD~3 HEADCompare commits
HEAD^nNth parent of merge commitgit show HEAD^2Analyze merge parents
HEAD^0Current commit (dereference tag)git show HEAD^0Ensure commit object
HEAD@{1}Previous HEAD positiongit checkout HEAD@{1}Undo last move
HEAD@{n}Nth previous state from refloggit reset --hard HEAD@{2}Recover state
branch@{1}Previous state of a branchgit checkout main@{1}Restore branch state
HEAD^@All parents of commitgit log HEAD^@List all parents
HEAD^!Only the commit itself (exclude parents)git log HEAD^!Isolate commit

What is HEAD in Git (Simple Explanation)

What does HEAD mean in Git

In Git, HEAD is a pointer to your current commit.

  • It usually points to the latest commit of your current branch
  • When you switch branches, HEAD moves to that branch, which is covered in detail in git branch basics.
  • When you make a new commit, HEAD automatically updates

Example:

text
HEAD -> main -> commit D

Here:

  • HEAD points to branch main
  • main points to the latest commit (D)

You can verify HEAD using git show command:

bash
git log --oneline

or use the git show command to inspect the current commit

bash
git show HEAD

HEAD~ vs HEAD^ (Quick Difference)

HEAD~ vs HEAD^ in one line

  • HEAD^ → immediate parent of current commit
  • HEAD~n → nth ancestor in a straight line

Shortcut:

text
HEAD~1 = HEAD^
HEAD~2 = parent of parent

When to use ~ vs ^

Use HEAD^ when dealing with parent commits, especially in merge scenarios:

bash
git show HEAD^
  • Refers to the first parent
  • In merge commits:
    • HEAD^1 → main branch parent
    • HEAD^2 → merged branch parent

Use HEAD~ when navigating commit history linearly:

bash
git log HEAD~3

This command moves back multiple commits

Simple Example (for clarity)

Assume commit history:

text
A → B → C → D (HEAD)

Then:

text
HEAD    = D
HEAD^   = C
HEAD~1  = C
HEAD~2  = B
HEAD~3  = A

Key takeaway

  • Use HEAD^ → when working with parent relationships (especially merges)
  • Use HEAD~ → when moving back in commit history step-by-step

How HEAD~ Works in Git

HEAD~1, HEAD~2 explained

The tilde (~) operator is used to move backward in commit history along a single line.

  • HEAD~1 → one commit before HEAD (same as HEAD^)
  • HEAD~2 → two commits before HEAD
  • HEAD~3 → three commits before HEAD

Example:

text
A → B → C → D (HEAD)

Mapping:

text
HEAD    = D
HEAD~1  = C
HEAD~2  = B
HEAD~3  = A

Commands:

bash
git show HEAD~1
git log HEAD~2
git diff HEAD~1 HEAD

Traversing commit history using ~

You can use ~ to move across commits for different operations, including recovery scenarios explained in git reset examples, similar to how navigation works in git checkout command.

bash
git checkout HEAD~2
git reset --hard HEAD~1
git diff HEAD~3 HEAD

How HEAD^ Works in Git

HEAD^ vs HEAD^2 (merge commits explained)

The caret (^) operator refers to parent commits.

  • HEAD^ or HEAD^1 → first parent
  • HEAD^2 → second parent (used in merge commits)

Example of a merge commit, which is explained in detail in git merge examples:

text
      E---F (feature)
     /     
A---B---C---D (main, HEAD)

If D is a merge commit:

text
HEAD^1 = C   (main branch)
HEAD^2 = F   (merged branch)

Commands:

bash
git show HEAD^1
git show HEAD^2

Understanding HEAD~ vs HEAD^ with Diagram

The image shows how HEAD~ and HEAD^ behave differently when navigating Git commit history.

Understanding HEAD~ vs HEAD^ with Diagram

Left Side: HEAD~ (Linear History)

On the left, commits follow a straight line:

text
A → B → C → D (HEAD)
  • HEAD~1 → moves to commit C
  • HEAD~2 → moves to commit B
  • HEAD~3 → moves to commit A

Key idea:

  • HEAD~n moves backward step-by-step in a single line
  • It always follows the first-parent chain

Right Side: HEAD^ (Merge Commit)

On the right, a merge scenario is shown:

text
      F (feature)
       \
A → B → C → D (HEAD)

Here, D is a merge commit with two parents.

  • HEAD^1 → points to C (main branch)
  • HEAD^2 → points to F (feature branch)

Key idea:

  • HEAD^ selects a specific parent
  • ^1 = first parent (current branch)
  • ^2 = second parent (merged branch)

Key Difference Highlighted

  • HEAD~

    • Moves back in history linearly
    • Ignores merge structure
  • HEAD^

    • Selects parent commits
    • Useful for understanding merges

Frequently Asked Questions

1. What is HEAD in Git?

HEAD in Git is a reference that points to the current commit in your working branch. It usually points to the latest commit and moves forward as new commits are created.

2. What is the difference between HEAD~ and HEAD^?

HEAD^ refers to the immediate parent of the current commit, while HEAD~ refers to ancestors in a linear history (e.g., HEAD~2 means two commits before HEAD).

3. Is HEAD~ same as HEAD^?

Yes, HEAD~1 is the same as HEAD^. Both refer to the first parent of the current commit.

4. What is HEAD^2 in Git?

HEAD^2 refers to the second parent of a merge commit. It is useful when working with branches that have been merged.

5. What is HEAD@{1} in Git?

HEAD@{1} refers to the previous position of HEAD in the reflog. It is commonly used to recover earlier states of your repository.

6. How to use HEAD~ in Git?

You can use HEADn to refer to n commits before the current commit, for example, HEAD3 refers to the third ancestor commit.

7. When should I use HEAD^ vs HEAD~?

Use HEAD^ when dealing with parent commits, especially in merge scenarios, and use HEAD~ when navigating commit history linearly.

8. What is git reflog and how is it related to HEAD@{}?

git reflog tracks changes to HEAD over time, and HEAD@{n} allows you to access previous states of HEAD using this history.

Summary

  • HEAD points to the current commit in your working branch

  • HEAD~n is used to move backward in commit history step-by-step

  • HEAD^n is used to access specific parent commits, especially in merge scenarios

  • HEAD@{n} helps you recover previous states using git reflog tutorial

  • Use HEAD~ for navigating history

  • Use HEAD^ for understanding parent relationships in merges

  • Use HEAD@{} for recovery and tracking past positions, often combined with concepts explained in git reset (soft vs hard vs mixed)


Official Documentation

Deepak Prasad

Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels across development, DevOps, networking, and security, delivering robust and efficient solutions for diverse projects.