HEAD and ORIG_HEAD in Git
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
HEAD and ORIG_HEAD are both Git references, but they solve different problems. HEAD tells Git where you are now, while ORIG_HEAD is a short-term safety marker Git often writes before operations that may move history in a way you later want to undo.
That difference matters because recovery commands are much easier to reason about once you know which ref is current state and which ref is a saved previous state.
What HEAD Means
Most of the time, HEAD points symbolically to the current branch, and the branch points to the current commit.
You can inspect it with:
If you are on main, the .git/HEAD file usually contains:
That means HEAD is not usually a raw commit hash. It is a symbolic ref to the branch you currently have checked out.
When you commit on that branch, the branch ref moves forward, and therefore HEAD moves with it.
Detached HEAD
If you checkout a commit directly instead of a branch, HEAD becomes detached:
Now HEAD points directly to a commit. That is not an error. It just means you are no longer on a branch tip. You can inspect files, test history, and even commit, but those commits are not attached to a branch unless you create one.
Detached HEAD is common during:
- debugging old revisions
- '
git bisect' - release investigation
The warning exists because people often forget to save new commits they make in that state.
What ORIG_HEAD Means
ORIG_HEAD is a recovery ref. Git updates it before certain history-moving commands, especially ones that may need a quick rollback.
Typical commands that write it include:
- '
git merge' - '
git pull' - '
git rebase' - '
git reset'
You can inspect it with:
If you just performed a bad merge or reset, ORIG_HEAD often points to where you were immediately before that operation started.
A Useful Recovery Pattern
Suppose you run:
and then realize you removed the wrong commit. Git often saved the pre-reset position in ORIG_HEAD, so you can restore it:
The same idea often works after a merge:
This is why ORIG_HEAD is worth knowing. It acts like a recent checkpoint for risky commands.
HEAD Versus ORIG_HEAD
The operational distinction is simple:
- '
HEADis where you are now' - '
ORIG_HEADis where Git remembered you were before a significant move'
HEAD changes all the time as you checkout, commit, and reset. ORIG_HEAD changes only for certain commands, and later operations can overwrite it. That is why ORIG_HEAD is useful but temporary.
When reflog Is Better
If ORIG_HEAD no longer points to the place you need, use the reflog:
The reflog records many past positions of HEAD and branch refs. ORIG_HEAD is one recovery hook; reflog is the fuller movement history.
A practical rule is:
- use
HEADto refer to the current commit - use
ORIG_HEADfor quick recovery after a recent major operation - use
reflogwhen the recovery target is older orORIG_HEADhas already changed
Common Pitfalls
- Assuming
ORIG_HEADis updated for every Git command. - Treating detached
HEADas a broken repository state when it is often completely intentional. - Expecting
ORIG_HEADto be permanent even though later commands can overwrite it. - Forgetting that
git reflogis usually the better recovery tool for anything beyond the most recent major move.
Summary
- '
HEADrepresents your current position in Git history.' - Detached
HEADmeansHEADpoints directly to a commit instead of to a branch. - '
ORIG_HEADis a saved previous position Git often records before risky history changes.' - It can help undo a recent merge, pull, rebase, or reset.
- For broader recovery, use
git refloginstead of relying only onORIG_HEAD.
Related reading
- Helm V3 - Cannot find the official repo
- Homebrew Install Specific Version of Formula
- How do I change the URI (URL) for a remote Git repository?
- How do I delete a Git branch locally and remotely?
- How do I revert a Git repository to a previous commit?
- How do I undo the most recent local commits in Git?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.