Sub topic 10
Undoing a reset with the reflog and ORIG_HEAD
This chapter assumes you can use git log, git status, and the three git reset modes. A reset moves a branch, but it does not delete the commits it leaves behind. Those commits stay in the object database for a while, and Git keeps two ways to find them: the reflog and ORIG_HEAD.
Why a reset is usually recoverable
A commit is removed from a branch's history when you reset past it. The commit object still exists until Git's garbage collection prunes it. By default, unreachable commits are kept for at least 30 days (the gc.reflogExpireUnreachable setting, which defaults to 30 days). Recovery means finding the hash of the old tip and pointing the branch at it again.
There is one limit. Changes that were never committed, such as edits that git reset --hard overwrote in the working directory, are not in any commit. Reflog cannot bring those back.
Using ORIG_HEAD
Before git reset moves the branch, it saves the old value of HEAD in a reference named ORIG_HEAD. This is the quickest way to undo the most recent reset.
Start from the three-commit repository built earlier, then reset too far:
git reset --hard HEAD~2
git log --onelineThe log now shows only the first commit. To return to where you were:
git reset --hard ORIG_HEAD
git log --onelineAll three commits are back. ORIG_HEAD is overwritten by the next operation that sets it, including another reset, a merge, or a rebase. Use it right away, not later.
Using the reflog
The reflog is a local log of every position HEAD has had. Each entry has a name such as HEAD@{1}, where the number counts back in time.
git reflogThe output looks like this:
a1b2c3d (HEAD -> main) HEAD@{0}: reset: moving to HEAD~2
f4e5d6c HEAD@{1}: commit: third
9a8b7c6 HEAD@{2}: commit: second
5d4c3b2 HEAD@{3}: commit (initial): firstThe entry at HEAD@{1} is the state before the reset. Move the branch back to it:
git reset --hard HEAD@{1}You can also use the hash directly, for example git reset --hard f4e5d6c. Choose the entry you want by reading the message on each line, not by counting positions. If you ran several commands since the mistake, the right entry will be further back than HEAD@{1}.
Inspecting before you restore
A hard reset to recover can itself overwrite current work. To be safer, look first:
git log --oneline f4e5d6c
git show f4e5d6cIf you only want to keep the lost commits without moving your branch, create a new branch at that hash:
git branch rescue f4e5d6cThis changes nothing in your working directory and gives the commits a name, so garbage collection cannot remove them.
Limits of the reflog
- It is local. A fresh clone has no history of your earlier resets, and nothing is shared when you push.
- Entries expire. Reachable entries last 90 days by default and unreachable ones 30 days.
- Each branch also has its own reflog, viewable with
git reflog show branch-name. This is useful when the mistake happened on a branch that is not checked out.
Exercise
You ran git reset --hard HEAD~3 by mistake and then ran git commit --allow-empty -m "test". Will git reset --hard ORIG_HEAD restore your lost commits? If not, what do you do?
Show answer
Probably not. ORIG_HEAD is set by operations such as reset, merge, and rebase, and a plain commit does not normally change it, so it may still point to the old tip. But you cannot be sure it was not overwritten by anything else you ran. The reliable method is git reflog: find the entry from before the reset (for example HEAD@{2}, since the empty commit added an entry), check it with git log --oneline <hash>, then run git reset --hard <hash>. Note that this discards the empty "test" commit from the branch.