THE GITHTML FIELD GUIDE
Understand HEAD, HEAD~1, and HEAD^2 in Git
HEAD identifies your current commit, usually through the checked-out branch. HEAD~1 follows the first parent once; HEAD~2 follows that first-parent chain twice. HEAD^2 selects the second parent of a merge commit, not the second commit back. Verify revision expressions with read-only commands before using them in reset, restore, or other changes.
Distinguish distance from parent selection
The tilde syntax walks repeatedly through first parents. The caret syntax selects a particular parent at one step. For an ordinary commit with one parent, HEAD~1 and HEAD^ identify the same ancestor. At a merge, the difference becomes important: HEAD^2 names the other parent of that merge, while HEAD~2 continues backward along the first-parent chain.
A root commit has no parent, and a shallow repository can lack ancestors beyond its boundary. An expression failing to resolve can therefore reflect the available history rather than a typo. Also, HEAD can directly name a commit in detached state instead of referring through a branch. The expression still identifies history, but a later commit may not advance a named branch.
Inspect the target without modifying anything
The examples assume HEAD has enough history and, for HEAD^2, is a merge commit. Quote expressions to avoid shell interpretation. Read the commit subjects and parent list before reusing an expression in a state-changing operation. If the expression does not resolve, stop and inspect the repository instead of substituting a guessed number.
- Draw the relevant parent relationship on paper when a merge is involved. Two parent choices represent different histories, not interchangeable previous versions.
- Prefer an explicit verified commit identifier in an operational handoff, because HEAD-relative expressions can resolve differently after the branch moves.
git show --no-patch --format='%h %p %s' HEAD
git show --no-patch --oneline 'HEAD~1'
git show --no-patch --oneline 'HEAD~2'
git show --no-patch --oneline 'HEAD^2'Use revision language carefully in recovery
An instruction to undo the last two commits is ambiguous if the latest commit is a merge or the branch has moved since the instruction was written. Identify the desired resulting snapshot and history relationship first. A revision expression locates a commit; it does not determine whether reset, revert, or restoring one path is the appropriate operation.
For example, reviewing the second parent of a merge is a harmless inspection, while resetting a shared branch there would discard the first-parent line from that branch’s tip and create a major collaboration problem. Keep target selection separate from action selection. Verify the full identifier, preserve important local state, and explain why that target is correct before any recovery command. Understanding parent syntax reduces risk precisely because it makes these differences visible.
An abbreviated identifier should be long enough to resolve uniquely in the repository. When communicating an important recovery target, the complete identifier avoids later ambiguity as history grows.
Sources and further reading
AI-assisted writing with source-linked guidance and illustrative examples. Read our editorial approach or report a correction.