Skip to content
Tutorials Center
My learning

Topics

Git Reference ยท Revisions and Pathspecs

Caret and tilde ancestor syntax

Reference for Git ancestor syntax: HEAD^, HEAD~3, ^2 for merge parents, chained forms like HEAD~2^2, ^0 and how ~ and ^ differ, with runnable examples.

Two suffixes walk backward through history from any commit name. ~ goes back along the first parent a number of times. ^ selects a specific parent of one commit. On a straight line of commits they give the same answer; they differ only at merge commits, which have two or more parents.

You can chain them: HEAD~2^2 means "go back two first-parent steps, then take the second parent of that commit". They work anywhere a revision is accepted.

Terminal
git show HEAD~3        # three commits back, following first parents
git show HEAD^2        # the second parent of a merge commit
git diff main~2 main   # changes from two commits ago to now

Try it

Bash Walk through a history with a merge

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Differences and error cases

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
<rev>^         # first parent of <rev>
<rev>^<n>      # n-th parent of <rev> (1-based)
<rev>^0        # <rev> itself, with tags peeled to the commit
<rev>~         # same as <rev>~1
<rev>~<n>      # n-th generation ancestor, following first parents only
Form Meaning
C^ or C^1 The first parent of C: for a merge, the branch you were on when you merged.
C^2 The second parent: for a merge, the tip of the branch that was merged in.
C^n The n-th parent. Octopus merges can have more than two.
C^0 C itself. Useful to get a commit from a tag name.
C~n Go back n steps along first parents. C~0 is C; C~ is C~1.
C^^ Same as C~2. Repeated ^ stacks.

Equivalences: C~3 is the same as C^^^, which is the same as C^1^1^1. C~2^2 is not the same as C^2~2.

Errors: asking for a parent that does not exist (HEAD^2 on a non-merge commit, HEAD~10 before the root) fails with fatal: ambiguous argument ... unknown revision.

Usage notes

  • Read left to right. A~2^2 means: start at A, go to its grandparent along first parents, then take the second parent of that commit.
  • First parent is the mainline. After git merge feature on main, ^1 is where main was before, and ^2 is the tip of feature. Using --first-parent in git log follows the same line as ~.
  • ^ after a fast-forward. A fast-forward creates no merge commit, so every commit has a single parent and ^2 does not exist.
  • Shell quoting. ^ is special in some shells (cmd.exe, PowerShell), and ~ expands to your home directory when it stands alone at the start of a word in Bash. Quote revisions like 'HEAD^' on Windows and avoid a leading ~.
  • Different meaning with ^ as prefix. ^C at the start means "exclude C and its ancestors" in a range; it is unrelated to the suffix form. See ^rev and --not.
  • ^{...} is different again. C^{tree} and C^{} peel objects to a type; see ^{type} and ^{}.
  • Combine with paths. HEAD~2:src/app.js names the file as it was two commits ago; see rev:path and :N:path.