Sub topic 10
Undoing a reset with the reflog and ORIG_HEAD
This chapter assumes you know what --soft, --mixed, and --hard do, and that you can read git log --oneline output. A reset that moved your branch to the wrong place feels destructive, but in most cases the old commits are still in the repository. You only need to find them again. Git gives you two tools for that: the ORIG_HEAD shortcut and the reflog.
Why a reset is usually recoverable
git reset changes where a branch points. It does not delete commit objects. After git reset --hard HEAD~1, the dropped commit still exists in the object database, but no branch points to it. Git calls such a commit unreachable. It stays on disk until garbage collection removes it, which by default happens weeks later.
Recovery therefore has two parts:
- Find the hash of the commit the branch used to point to.
- Point the branch (or a new branch) at that hash again.
ORIG_HEAD and the reflog are two ways to do step 1.
The exception is uncommitted work. A hard reset overwrites changes in tracked files that were never committed, and neither tool can bring those back. The section on limits below covers this.
ORIG_HEAD: the quick undo
Before git reset moves HEAD, it saves the old position in a ref named ORIG_HEAD. You can see it directly:
git rev-parse HEAD
git reset --hard HEAD~1
git rev-parse ORIG_HEADThe first and third commands print the same hash. To go back to where you were:
git reset --hard ORIG_HEADORIG_HEAD is a convenience for the very last dangerous operation. Several other commands also write it, including git merge, git rebase, and git pull. Each new write replaces the previous value. Run git reset, then git pull, and ORIG_HEAD now describes the pull, not your reset.
That makes ORIG_HEAD reliable only when you use it immediately. If anything else has happened in between, use the reflog.
The reflog: a history of where HEAD has been
The reflog is a local log of every position a ref has had. Git appends an entry each time HEAD or a branch tip moves because of a commit, checkout, merge, rebase, reset, or similar operation. Plain git log follows parent links from the current commit, so it cannot show commits that are no longer reachable. The reflog follows time, not ancestry, so it can.
Start with the demo repository from earlier in the tutorial, which has three commits ("first", "second", "third"). Run a hard reset and then inspect the reflog:
git reset --hard HEAD~1
git reflogThe output looks like this (your hashes will differ):
5c1e9a2 (HEAD -> main) HEAD@{0}: reset: moving to HEAD~1
b83d07f HEAD@{1}: commit: third
5c1e9a2 (HEAD -> main) HEAD@{2}: commit: second
e41a6bd HEAD@{3}: commit (initial): firstEach line has four parts:
- The abbreviated hash that
HEADpointed to after the event. - The reflog selector,
HEAD@{n}. Entry 0 is the newest. - The kind of event, such as
commit,reset,checkout, ormerge. - A description, for example the commit message or the reset target.
The commit "third" (b83d07f) is at HEAD@{1}. Move the branch back to it:
git reset --hard HEAD@{1}
git log --onelineAll three commits are back, and the working directory matches the third commit again.
Reading the reflog safely
HEAD@{1} means "where HEAD was one move ago". It is not "the commit before this one". If you ran another command after the bad reset, such as a git checkout or a new commit, the number you need is different. Read the whole reflog and pick the entry by its description, not by guessing the index.
Two options make the reflog easier to read:
git reflog --date=iso
git reflog show main--date=isoreplaces the@{n}selectors with timestamps, which helps when you remember roughly when the mistake happened.git reflog show mainshows the reflog for themainbranch only.git reflogby default shows the reflog forHEAD.
You can also use time-based selectors anywhere Git accepts a commit:
git show HEAD@{10.minutes.ago}
git reset --hard main@{yesterday}Treat these with care. The time is interpreted using entries in your local reflog, so the result depends on when your own operations happened.
A safer recovery: inspect before you reset
Running git reset --hard HEAD@{1} as your first reaction has a problem: a second hard reset can discard changes you made after the first one. A safer habit is to create a new branch at the lost commit and look at it first:
git branch rescue HEAD@{1}
git log --oneline rescueThe branch rescue now makes the old commits reachable again, and your current branch is untouched. From here you have choices:
- Run
git reset --hard rescueonce you are sure you want the branch back there. - Run
git cherry-pick <hash>to bring over only one lost commit. - Run
git diff main rescueto see exactly what the reset removed.
Keeping a branch name on the lost commit also protects it from garbage collection, because the commit is reachable again.
ORIG_HEAD versus the reflog
| Aspect | `ORIG_HEAD` | Reflog |
|---|---|---|
| What it stores | One previous position | Many positions, newest first |
| Overwritten by | The next reset, merge, rebase, or pull | Never overwritten; old entries expire |
| Best for | Undoing the last operation right away | Recovering after several steps |
| Syntax | `git reset --hard ORIG_HEAD` | `git reset --hard HEAD@{n}` |
| Shows description of events | No | Yes |
Limits you need to know
Uncommitted changes are not in the reflog
The reflog records commits and ref movements. If you had edited tracked files without committing and then ran git reset --hard, those edits are gone. Resetting back to the old commit restores only what was committed.
There is one partial exception. If you had staged a change with git add, Git wrote the file contents into the object database as a blob. You can sometimes list such orphaned blobs with:
git fsck --lost-foundThis prints dangling blobs and commits and writes them to .git/lost-found/. You can open the files and copy content back by hand. Changes that were never staged do not appear there. Untracked files are never touched by git reset --hard in the first place, so they are not lost by it.
The reflog is local
The reflog lives in .git/logs/ and is never pushed or cloned. A teammate cannot use your HEAD@{1}, and a fresh clone has almost no reflog. If you delete the repository folder, the reflog goes with it.
Branch reflogs are removed when you delete a branch. The HEAD reflog keeps its entries, so after deleting a branch by mistake you can still find its last commit with git reflog.
Entries expire
Reflog entries are pruned by git gc. The defaults are:
- 90 days for entries whose commits are still reachable (
gc.reflogExpire). - 30 days for entries whose commits are no longer reachable (
gc.reflogExpireUnreachable).
After pruning, the commits themselves can be deleted. A command such as git reflog expire --expire=now --all followed by git gc --prune=now removes the safety net immediately. Do not run these commands while you still might need to recover something.
A recovery checklist
When a reset goes wrong, work through these steps:
- Stop. Do not run more commands that move
HEADuntil you know the target hash. - Run
git statusto see whether any uncommitted work is still present. - If the reset was your last operation, check
git rev-parse ORIG_HEAD. - Otherwise run
git reflogand find the entry that was current before the mistake. Use the event description to confirm it. - Create a branch there with
git branch rescue <hash>and inspect it. - Move your branch with
git reset --hard rescueor copy over single commits withgit cherry-pick.
Exercise
You ran git reset --hard HEAD~2, then git pull, and now you realise the reset removed two commits you wanted. Why is git reset --hard ORIG_HEAD the wrong fix, and what should you do instead?
Show answer
git pull writes ORIG_HEAD as well, so it no longer holds the position from before the reset. Resetting to it would move you to where the branch was before the pull, not before the reset. Run git reflog, find the entry that shows the commit from before reset: moving to HEAD~2, and create a branch there with git branch rescue <hash>. Then merge, cherry-pick, or reset to it as needed.
Exercise
A teammate says: "I ran git reset --hard with unsaved edits to a tracked file, so I'll just restore it from the reflog." Will that work? Explain.
Show answer
No. The reflog records where refs pointed, so it can restore commits. Edits that were never committed are not in any commit. If the changes had been staged with git add, the file contents might still exist as a dangling blob that git fsck --lost-found can find. Edits that were never staged are lost.