Naming Commits: HEAD, ~ and ^

Updated · 12 min read
easyGit 2.53githeadrevisionsrev-parseinterview

In a family, you don't need anyone's full legal name to point to them. "My mother" or "my grandfather on my father's side" is enough, because everyone knows where to start and which way to go.

Git works the same way. Every commit has a 40-character ID, but you rarely type it. You can say "the commit I am on" (HEAD), "one before that" (HEAD~1), or "the second parent of that merge" (HEAD^2), and Git works out which commit you mean. Every command that takes a commit, such as git show, git diff, git log and later git reset and git rebase, understands these names.

In this lesson, you will learn every everyday way to name a commit, and how to check which commit a name points to.

A History with a Merge

To see all the names in action, the history needs a merge: a commit with two parents. Advitha built the receipts feature on a separate branch while Navika kept working on main, and then the two lines of work were joined. Branches and merges have their own lessons later. Here, only the shape of the history matters:

Terminal
mkdir fee-tracker
cd fee-tracker
git init --quiet
echo "Fee Tracker" > README.md
git add README.md
git commit --quiet -m "Add project README"
echo 'Term 1 fee: $12,000' > fees.txt
git add fees.txt
git commit --quiet -m "Add the term fees"
echo 'Late fee: $250' >> fees.txt
git commit --quiet -am "Add the late fee"
git switch --quiet -c receipts
echo "Receipt No. 1" > receipts.txt
git add receipts.txt
git commit --quiet --author="Advitha Anamaneni <advitha@example.com>" -m "Add receipt numbers"
echo "Print after payment" >> receipts.txt
git commit --quiet -a --author="Advitha Anamaneni <advitha@example.com>" -m "Print a receipt after payment"
git switch --quiet main
echo 'Hostel fee: $9,000' >> fees.txt
git commit --quiet -am "Add the hostel fee"
git merge --quiet --no-ff -m "Merge the receipts feature" receipts
echo 'Exam fee: $800' >> fees.txt
git commit --quiet -am "Add the exam fee"
git log --oneline --graph
Output
* 8a7d198 (HEAD -> main) Add the exam fee
*   776ef76 Merge the receipts feature
|\
| * 685de2a (receipts) Print a receipt after payment
| * 932b53a Add receipt numbers
* | ec97acb Add the hostel fee
|/
* 4c2a088 Add the late fee
* 4009644 Add the term fees
* dd34d8a Add project README

--graph draws the history from top (newest) to bottom (oldest). Each * is a commit. The two lines show that the merge commit has two parents: the hostel fee commit on main, and Advitha's last commit on receipts.

Full and Short IDs

Every commit's real name is its full ID, 40 hexadecimal characters. Any unique beginning of it works as well, which is why the 7-character short IDs from git log --oneline are enough. git rev-parse turns any name into the full ID, so you can always check what a name means:

Terminal
git rev-parse HEAD
git rev-parse --short HEAD
Output
8a7d19829b269b0964b403ee9f4935094c59725b
8a7d198

Git needs at least 4 characters, and as many as it takes to be unique in your repository. In a very large project, Git shows longer short IDs on its own.

HEAD and Branch Names

HEAD means "the commit you are on now". Usually, HEAD points to a branch, and the branch points to its newest commit. That is what (HEAD -> main) in git log says. A branch name on its own means the newest commit of that branch:

Terminal
git show -s --format="%h %s" HEAD
git show -s --format="%h %s" main
git show -s --format="%h %s" receipts
Output
8a7d198 Add the exam fee
8a7d198 Add the exam fee
685de2a Print a receipt after payment

HEAD and main are the same commit because main is the current branch. receipts is Advitha's last commit. @ on its own is a short way to write HEAD.

Going Back: ~

~n means "go back n commits, always following the first parent". HEAD~1 is the commit before HEAD, HEAD~2 the one before that, and so on. The small loop below prints each name next to the commit it points to:

Terminal
for name in HEAD HEAD~1 HEAD~2 HEAD~3 HEAD~4 HEAD~5; do
  printf "%-8s " "$name"
  git show -s --format="%h %s" "$name"
done
Output
HEAD     8a7d198 Add the exam fee
HEAD~1   776ef76 Merge the receipts feature
HEAD~2   ec97acb Add the hostel fee
HEAD~3   4c2a088 Add the late fee
HEAD~4   4009644 Add the term fees
HEAD~5   dd34d8a Add project README

Notice that HEAD~2 is the hostel fee commit, not Advitha's work. At the merge, ~ follows the first parent, which is the branch that was merged into (main). Advitha's commits are on the second parent's side, so ~ never reaches them.

HEAD~ with no number means HEAD~1. A name ending in ~ works on any commit: receipts~1 is the commit before the newest one on receipts.

Choosing a Parent: ^

^n means "the nth parent of this commit". Most commits have one parent, so ^ alone (^1) is the same as ~1. The difference appears at a merge, which has two parents:

Terminal
for name in HEAD~1 HEAD~1^1 HEAD~1^2 HEAD~1^2~1; do
  printf "%-11s " "$name"
  git show -s --format="%h %s" "$name"
done
Output
HEAD~1      776ef76 Merge the receipts feature
HEAD~1^1    ec97acb Add the hostel fee
HEAD~1^2    685de2a Print a receipt after payment
HEAD~1^2~1  932b53a Add receipt numbers
NameMeansHere
HEAD~1One commit back from HEADThe merge commit
HEAD~1^1The merge's first parent (the branch merged into)"Add the hostel fee"
HEAD~1^2The merge's second parent (the branch that was merged)Advitha's "Print a receipt after payment"
HEAD~1^2~1One back from that, on Advitha's side"Add receipt numbers"
~ follows first parents, ^2 takes the second parent of a merge

You can mix them in one name, and ^^ works like ~2: HEAD^^ is the same commit as HEAD~2.

Syntax

NameMeans
a1b2c3dThe commit whose ID starts with these characters
HEAD or @The current commit
mainThe newest commit of the branch main
<name>~<n>n commits back, following first parents
<name>^<n>The nth parent (^ alone is ^1)
<name>@{<n>}Where the branch (or HEAD) pointed n moves ago
@{u}The upstream branch of the current branch, such as origin/main
:/<text>The newest commit whose message contains the text
<name>:<path>A file as it is in that commit

Earlier Positions: @{n}

Git keeps a diary, the reflog, of every place HEAD has pointed in your repository: after each commit, switch, merge and reset. HEAD@{n} means "where HEAD was n moves ago":

Terminal
git reflog -4
Output
8a7d198 (HEAD -> main) HEAD@{0}: commit: Add the exam fee
776ef76 HEAD@{1}: merge receipts: Merge made by the 'ort' strategy.
ec97acb HEAD@{2}: commit: Add the hostel fee
4c2a088 HEAD@{3}: checkout: moving from receipts to main

Read it from the top: HEAD@{0} is now, HEAD@{1} is just before the exam fee commit (the merge), and so on. HEAD@{3} is not a commit you made at all: it records the moment you switched back from receipts to main. This is different from ~. HEAD~2 follows the history of commits, while HEAD@{2} follows the history of your actions. After a reset or a rebase the two give very different answers, and that is exactly what makes the reflog useful for recovering lost work. The lessons on recovering work use it a lot.

Branches have a reflog too, so main@{1} is where main pointed before its last move. A date also works, as in main@{yesterday} or main@{2026-01-05}.

@{u} (short for @{upstream}) names the remote branch that your current branch tracks, such as origin/main. It needs a remote repository, so it is explained in the lessons on remotes. git log @{u}.. is a common way to see which of your commits are not pushed yet.

Find a Commit by Its Message: :/

When you remember the message but not the ID, :/ followed by some text names the newest commit whose message contains it:

Terminal
git show -s --format="%h %s" ":/hostel"
git diff --stat ":/late fee" ":/hostel"
Output
ec97acb Add the hostel fee
 fees.txt | 1 +
 1 file changed, 1 insertion(+)

Put the name in quotes when the text has spaces.

Common Mistakes

Expecting HEAD~2 to reach a branch that was merged in. ~ follows only first parents. Use ^2 to step onto the merged branch.

Using HEAD^2 on an ordinary commit. It has only one parent, so Git answers with an error such as unknown revision. You probably meant HEAD~2.

Mixing up HEAD~2 and HEAD@{2}. The first is two commits back in the history. The second is where HEAD was two actions ago, which after a reset may be a commit no branch shows anymore.

Forgetting quotes in some shells. In zsh and in Windows PowerShell, ^, @{} and ~ can mean something to the shell. If a name gives a strange error, put it in quotes: git show "HEAD^2".

Copying too few characters of an ID. In a big repository, 4 or 5 characters may match several commits. Git then says the name is ambiguous. Use 7 or more.

Under the Hood

Every name is turned into a full ID before the command runs, and git rev-parse shows you the result. A branch name is looked up in .git/refs/heads/ (or in the packed list .git/packed-refs), HEAD is read from .git/HEAD, and each ~ or ^ reads the commit object and follows one of its parent lines. A merge commit simply has two parent lines, and ^2 picks the second:

Terminal
git cat-file -p HEAD~1
Output
tree c4062f21f6137adee056acbc2ce72bf12120424f
parent ec97acb98f4875aa45c7f40f5a9a83de426ceda9
parent 685de2a4f638949d6847a1ec80101b8bfd7ccbb8
author Navika Anamaneni <navika@example.com> 1767630060 -0600
committer Navika Anamaneni <navika@example.com> 1767630060 -0600
 
Merge the receipts feature

The first parent is the hostel fee commit and the second is Advitha's last commit, exactly as HEAD~1^1 and HEAD~1^2 found them.

Interview Corner

What is HEAD in Git?

A pointer to the commit you are on. Normally it points to the current branch, which points to that branch's newest commit, so a new commit moves the branch and HEAD with it. If HEAD points directly to a commit instead of a branch, you are in "detached HEAD" state.

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

HEAD~2 is two generations back, following the first parent each time: the grandparent. HEAD^2 is the second parent of HEAD, which only exists when HEAD is a merge commit. HEAD~1 and HEAD^1 are always the same commit.

What is the difference between HEAD~1 and HEAD@{1}?

HEAD~1 is the parent of the current commit, from the commit history. HEAD@{1} is wherever HEAD pointed before your last action, from the reflog. After an ordinary commit they are the same, but after a reset, rebase or switch they can be completely different.

How do you see the full ID of a commit name like main~3?

git rev-parse main~3. Add --short for the abbreviated form. git rev-parse resolves any revision name, which also makes it useful in scripts.

What does @{u} mean?

The upstream branch of the current branch, the remote branch it tracks, such as origin/main. For example, git log @{u}..HEAD lists the local commits that have not been pushed.

Practice

Challenge: name it. In the same repository, write a name that starts at receipts and reaches "Add the late fee", the commit Advitha's branch started from. Then check that it gives the same ID as HEAD~3.

What does Git print? Before you open the solution, count the steps back from Advitha's last commit, and decide whether you need ~ or ^.

Show solution
Terminal
git show -s --format="%h %s" receipts~2
git rev-parse --short receipts~2
git rev-parse --short HEAD~3
Output
4c2a088 Add the late fee
4c2a088
4c2a088

receipts has only one-parent commits, so ~ is enough: two steps back from "Print a receipt after payment" is "Add the late fee". HEAD~3 reaches the same commit along the main side, so the two git rev-parse commands print the same short ID. receipts^^ would work too.

Checkpoint. In this repository, find each of these with one name and check it with git rev-parse --short: the first commit, Advitha's first commit, and the commit HEAD pointed to two actions ago.

Quick check: Naming commits

4 questions
1Which name means "the parent of the current commit"?
2HEAD is a merge commit. Which name reaches the branch that was merged in?
3What does HEAD@{1} use to find a commit?
4Which command prints the full ID for any commit name?

Key Takeaways

Quick reference

NameExamplePoints to
Short IDgit show dd34d8aThe commit starting with dd34d8a
HEAD / @git show HEADThe current commit
Branchgit log receiptsThe newest commit on receipts
~ngit diff HEAD~3 HEADn commits back, first parents
^ngit show HEAD^2The nth parent of a merge
@{n}git show HEAD@{2}Where HEAD was n actions ago
@{u}git log @{u}..The upstream branch
:/textgit show ":/hostel"Newest commit whose message has the text
git rev-parsegit rev-parse --short HEAD~2Prints the ID a name resolves to

Was this tutorial helpful?

Lesson 21 of 106