Sub topic 5
Addressing entries with HEAD@{n} and time-based selectors
This chapter assumes you can read git reflog output, know that HEAD and branches are pointers to commits, and have a terminal open in a repository with some history.
Why selectors exist
Copying commit hashes out of the reflog works, but it is slow and easy to get wrong. Git lets you name a reflog entry directly with a selector. A selector is an expression that Git resolves to a commit, so you can pass it to any command that accepts a revision, such as git show, git log, git reset or git branch.
Position selectors: ref@{n}
The form HEAD@{n} means "where HEAD pointed n moves ago". HEAD@{0} is the current position, HEAD@{1} is the position before the latest move, and so on.
git reflog
# 3f2a9c1 HEAD@{0}: reset: moving to HEAD~2
# 9d4e7b8 HEAD@{1}: commit: Add export feature
# c81b0aa HEAD@{2}: commit: Fix parser bug
git rev-parse HEAD@{1}
# prints the full hash that starts with 9d4e7b8git rev-parse prints the commit a selector resolves to. It is a safe way to check a selector before you use it.
The number counts entries in the reflog, not commits in history. After a reset, HEAD@{1} is not the parent of HEAD. It is the place HEAD was before the reset.
Branch selectors
You can put a branch name in front of the braces. main@{1} reads the reflog of main, not the reflog of HEAD. The two logs differ, because the branch log only records moves of that branch tip.
git reflog show main
git rev-parse main@{1}Use HEAD@{n} when you are retracing what you did in your working session. Use branch@{n} when you care about one branch's tip.
Time-based selectors
You can also ask where a ref pointed at a point in time. Git accepts several forms.
git rev-parse main@{yesterday}
git rev-parse main@{2.hours.ago}
git rev-parse HEAD@{2026-12-03}
git reflog show main --date=relativeThe --date=relative option changes the reflog display so entries show "2 hours ago" instead of @{n} numbers. This helps you pick a time to use.
Common errors
Time selectors rely on this repository's reflog, so they reflect when you did things, not when commits were authored. A commit you made last week but checked out five minutes ago appears under a recent time for HEAD.
- Asking for a time before the log starts. If the reflog has no entry that old, Git warns that the log only goes back to a certain date, and it gives you the oldest entry it has. Do not trust that result as the answer for that time.
- Shell expansion of braces. In some shells,
{and}have special meaning. If a selector fails, quote it:git show 'HEAD@{1}'. In PowerShell, quoting is usually required. - Confusing
HEAD~1withHEAD@{1}. The first is the parent commit. The second is the previous reflog position. They often differ. - Numbers shift. Every new move adds an entry at
@{0}, soHEAD@{1}today will not mean the same thing after your next commit. Resolve it to a hash if you need a stable reference.
Exercise
You ran git reset --hard HEAD~3 by mistake and then made no other changes. Which selector names the commit you were on before the reset, and how does it differ from HEAD~3?
Show answer
HEAD@{1} names it. It is the previous reflog position, which is the commit HEAD pointed to before the reset. HEAD~3 is the commit three parents back from the current HEAD, which is where you are now after the reset.