Naming Commits: Revision Syntax, Ancestry, Ranges and Reflog References
Every way to name a commit in Git: IDs, branches, tags and HEAD, ancestry with ~ and ^, upstream and push references, reflog and time-based names, searching by message, files at a revision, and ranges with .., ... and ^ for log, diff and rev-list.
kvi5hnuCode Sprout team member7 min readOn this page
Commands like log, show, diff, reset, switch and rebase all take revisions: expressions that name a commit, or a set of commits. Git's revision syntax is compact, and once you know it you can say "the second parent of the merge before last" or "commits on my branch that aren't on main" in a few characters. This article collects the forms you'll use, with git rev-parse to show what each resolves to.
Names for one commit
| Form | Meaning |
|---|---|
7fb62a0, or the full 40-character ID | a commit by ID. At least 4 characters, and enough to be unique |
main, feature/maps | the commit a branch points to |
origin/main | a remote-tracking branch |
v1.0 | a tag, or the commit it points to |
HEAD | the current commit. @ alone means HEAD |
ORIG_HEAD, MERGE_HEAD, FETCH_HEAD | special references set by reset, merge, rebase and fetch |
When a name is ambiguous, Git looks for it in this order: .git/<name> (such as HEAD), then tags, then branches, then remote-tracking branches. A tag and a branch both named v1 trigger a warning. Write refs/heads/v1 or refs/tags/v1 to be explicit.
git rev-parse <revision> prints the full ID a revision resolves to, and --short an abbreviated one. It's the way to check an expression before using it in a destructive command.
Ancestry: ~ and ^
~n walks back n first parents. ^n picks the nth parent of a merge commit:

git init -q trail-guide && cd trail-guide
git commit -q --allow-empty -m "A"
git commit -q --allow-empty -m "B"
git switch -q -c side
git commit -q --allow-empty -m "C1"
git commit -q --allow-empty -m "C2"
git switch -q main
git commit -q --allow-empty -m "D"
git merge -q --no-ff side -m "M"
for rev in HEAD HEAD~1 HEAD^ HEAD^1 HEAD^2 HEAD~2 HEAD^^ HEAD^2~1 HEAD~3; do
printf '%-9s -> %s\n' "$rev" "$(git log -1 --format=%s "$rev")"
done
HEAD -> M
HEAD~1 -> D
HEAD^ -> D
HEAD^1 -> D
HEAD^2 -> C2
HEAD~2 -> B
HEAD^^ -> B
HEAD^2~1 -> C1
HEAD~3 -> A
~is "go back through first parents", the branch's own line:HEAD~3is three commits back.~alone means~1.^alone is also the first parent, soHEAD^equalsHEAD~1.^2is the second parent, which exists only for merges: the tip of the branch that was merged in.- They combine left to right:
HEAD^2~1is "the second parent, then one back".HEAD~2^2would be the second parent of the commit two back, if that's a merge. - For ordinary commits,
~and^are interchangeable. They differ only at merges.
In some shells, ^ needs quoting: in Windows cmd, write HEAD^^ or "HEAD^", and in zsh with extendedglob, HEAD\^. ~ works everywhere, which makes it the common choice.
Upstream, push and previous-branch shortcuts
| Form | Meaning |
|---|---|
@{upstream} or @{u} | the current branch's upstream, such as origin/main |
main@{u} | main's upstream |
@{push} | where git push would push the current branch. Usually the same as @{u}, but differs in fork setups |
@{-1} | the branch you were on before, as in git switch - |
git log --oneline @{u}.. # my commits not yet on the upstream
git log --oneline ..@{u} # upstream commits I haven't merged
git rebase @{u} # rebase onto the upstream
git merge @{-1} # merge the branch I was just on
Reflog and time-based names
From the reflog (article 26):
| Form | Meaning |
|---|---|
HEAD@{2} | where HEAD was two moves ago |
main@{1} | where main pointed before its last change |
main@{yesterday}, HEAD@{2.hours.ago} | where the reference pointed at that time, in this repository |
main@{2026-03-01} | the same, with a date |
@{1} | the current branch's previous position |
main@{yesterday} uses the reflog, so it answers "where was my main yesterday?", not "what was committed before yesterday". For the latter, use git log --until=yesterday -1 main. Reflog names work only as far back as the reflog goes, 90 days by default.
Finding a commit by its message
| Form | Meaning |
|---|---|
:/fix login | the youngest commit, reachable from any reference, whose message matches the regular expression fix login |
main^{/fix login} | the youngest commit reachable from main whose message matches |
git show :/"Add lake"
git rebase -i "main^{/Release 2.0}"
These save looking up IDs in scripts and one-off commands. :/!-foo matches commits whose message does not match foo.
Trees, files and object types
A revision can name things other than commits:
| Form | Meaning |
|---|---|
HEAD:README.md | the file README.md as it is in HEAD |
v1.0:docs/ | the docs folder in v1.0 |
:README.md (or :0:README.md) | the staged version of the file |
:2:file and :3:file | "ours" and "theirs" during a merge conflict |
v1.0^{commit} | the commit an annotated tag points to (v1.0^0 is the same) |
HEAD^{tree} | the commit's top-level tree |
git init -q trail-guide && cd trail-guide
echo "Lake loop: 5 km" > lake.txt; git add .; git commit -q -m "Add lake page"
echo "Lake loop: 5.4 km" > lake.txt; git commit -q -am "Update distance"
echo "Lake loop: 5.4 km, flat" > lake.txt; git add lake.txt
echo "Lake loop: 5.4 km, flat, shady" > lake.txt
echo "HEAD~1:lake.txt -> $(git show HEAD~1:lake.txt)"
echo "HEAD:lake.txt -> $(git show HEAD:lake.txt)"
echo ":lake.txt -> $(git show :lake.txt) (staged)"
echo "working tree -> $(cat lake.txt)"
git cat-file -t HEAD^{tree}
HEAD~1:lake.txt -> Lake loop: 5 km
HEAD:lake.txt -> Lake loop: 5.4 km
:lake.txt -> Lake loop: 5.4 km, flat (staged)
working tree -> Lake loop: 5.4 km, flat, shady
tree
git show <rev>:<path> prints a file from any commit without changing anything, and git show <rev>:<path> > copy.txt saves it. Paths are relative to the repository root, unless they start with ./.
Ranges: sets of commits
Commands that list commits, such as log, rev-list, cherry-pick and format-patch, accept ranges:
| Range | Commits |
|---|---|
main..feature | reachable from feature but not from main: what the feature adds |
main...feature | reachable from either, but not both: the commits each side has that the other lacks |
feature ^main | the same as main..feature, in the general form |
feature --not main hotfix | from feature, excluding anything reachable from main or hotfix |
A^! | just commit A, not its ancestors |
M^@ | all parents of M |
M^- | M^..M: what merge M brought in. M^-2 excludes the second parent instead |
git init -q trail-guide && cd trail-guide
git commit -q --allow-empty -m "Start"
git switch -q -c feature; git commit -q --allow-empty -m "F1"; git commit -q --allow-empty -m "F2"
git switch -q main; git commit -q --allow-empty -m "M1"
echo "=== main..feature: on feature, not on main"
git log --format=%s main..feature
echo "=== feature..main: on main, not on feature"
git log --format=%s feature..main
echo "=== main...feature: either side, not both (with markers)"
git log --format='%m %s' --left-right main...feature
echo "=== counts: ahead/behind"
git rev-list --left-right --count main...feature
=== main..feature: on feature, not on main
F2
F1
=== feature..main: on main, not on feature
M1
=== main...feature: either side, not both (with markers)
< M1
> F2
> F1
=== counts: ahead/behind
1 2
A..Banswers "what's in B that isn't in A?", which is perfect for "what would this merge or push bring?". An empty side meansHEAD, soorigin/main..means "my commits not onorigin/main".A...Bwith--left-rightmarks each commit<(only in A) or>(only in B).git rev-list --left-right --count A...Bprints the two counts, which is howgit statuscomputes "ahead 2, behind 1".
Ranges mean something different in git diff
git diff compares two snapshots, not sets of commits, so the dots mean something else there:
| Command | Compares |
|---|---|
git diff A B or git diff A..B | snapshot A with snapshot B |
git diff A...B | the merge base of A and B with B: only B's changes since they diverged, which is what a pull request shows |
That's a common source of confusion: git log main..feature lists what the feature adds, and the matching diff is git diff main...feature, with three dots (article 7).
What's current
@as a short name forHEAD,@{u}and@{push}work in all current versions.- With SHA-256 repositories (article 50), IDs are 64 characters long, but abbreviated IDs and every form here work the same.
Common questions
What's the difference between HEAD~2 and HEAD^2?
HEAD~2 is the grandparent along first parents. HEAD^2 is the second parent of HEAD, which exists only if HEAD is a merge commit. For a non-merge commit, HEAD^2 is an error.
How do I see what I'm about to push?
git log --oneline @{push}.., or @{u}.. for the upstream. git diff @{u}... shows the combined changes.
How many characters of a commit ID do I need? Enough to be unique in the repository. Git shows 7 or more depending on the repository's size, and accepts as few as 4. In scripts and long-lived references, such as bug reports, use at least 12.
Why does main@{yesterday} say "log for 'main' only goes back to..."?
Reflog-based names only know what your repository recorded. A fresh clone has no history in its reflog. To find the last commit before a date, use git log --until=<date> -1.
Summary
- Commits are named by ID, branch, tag,
HEAD(@) and special references likeORIG_HEAD.git rev-parseresolves any name. ~nwalks back first parents,^npicks a merge's nth parent, and they chain:HEAD^2~1.@{u},@{push}and@{-1}name the upstream, push destination and previous branch.ref@{n}andref@{date}read the local reflog.:/textandrev^{/text}find commits by message.rev:pathnames a file in a commit, and:paththe staged version.A..Bis in B, not A.A...Bis in either, not both. Ingit diff, three dots compare from the merge base.
Practice questions
HEAD is a merge of feature into main. Which revision names the last commit of feature before the merge, and which names the previous main?
Answer
HEAD^2 is the second parent, the tip of feature that was merged in. HEAD^1, also written HEAD^ or HEAD~1, is the first parent, where main was before the merge.
Write commands that show which commits your branch has that its upstream doesn't, and which the upstream has that you don't.
Answer
git fetch
git log --oneline @{u}.. # mine, not on the upstream
git log --oneline ..@{u} # theirs, not merged yet
git rev-list --left-right --count HEAD...@{u} # both counts at once
How do you save config.yml as it was in tag v2.3 to a file, without changing your working tree?
Answer
git show v2.3:config.yml > config-v2.3.yml. The rev:path form reads the file from that commit. Alternatively, git restore --source=v2.3 config.yml replaces the working file itself.
Previous: Removing Untracked Files With git clean
Next: Who Changed This Line? git blame and Ignoring Noise Commits

More in Git Course
Quiz: Git Delicate Details
Twenty-five questions on Git's subtle corners: what commit -a and stash leave out, empty folders, ignore patterns and negation, HEAD~ versus HEAD^, ours and theirs in a rebase, tags that don't move or don't push, describe, merge reverts, diff exit codes, stash conflicts and more.
Quiz: Git Interview Questions
Thirty Git questions of the kind asked in developer interviews: Git versus GitHub, the staging area, merge versus rebase, fetch versus pull, reset, revert and restore, detached HEAD, conflicts, force pushing, stash, cherry-pick, reflog, bisect, submodules, LFS and how commits are identified.
Quiz: Git Course Final Exam (All Parts)
The final exam for the Git course: thirty questions across all twelve parts, from the three areas and staging through branching, remotes, undoing, history investigation, configuration, tags, larger projects, patches, rewriting, internals and troubleshooting.
Quiz: Professional Git Practice (Git Part 12)
Sixteen questions on migrating to Git from Subversion, TFVC and Mercurial, team best practices for commits, branches and reviews, repository hygiene, and diagnosing and fixing the most common Git errors, with explanations.
Discussion
Sign in to ask a question or share what you learned.