Sub topic 7
Recovering from git reset --hard
This chapter assumes you can commit, branch and switch, and that you know a branch is a pointer to a commit. It also assumes you can read git reflog output and use HEAD@{n} selectors from earlier chapters.
What the reset actually does
git reset --hard <target> moves the current branch to <target>. It also overwrites your index and working tree to match. The commits that the branch used to point to are not deleted. They are no longer reachable from the branch, so git log stops showing them. The reflog still records where HEAD was.
That is why a hard reset of committed work is recoverable. Uncommitted changes are a different case, covered in a later chapter on limits.
Step-by-step recovery
Suppose you reset too far.
git reset --hard HEAD~2
# Oops, those commits were needed.
git reflog
git reset --hard HEAD@{1}- Run
git reflog. The reset itself is the newest entry,HEAD@{0}. - Find the entry from just before the reset. Here that is
HEAD@{1}, the commit9d4e7b8. - Run
git reset --hard HEAD@{1}. The branch moves back to that commit, and your files return to that state.
The reflog before recovery looks like this:
git reflog
# 3f2a9c1 HEAD@{0}: reset: moving to HEAD~2
# 9d4e7b8 HEAD@{1}: commit: Add export feature
# c81b0aa HEAD@{2}: commit: Fix parser bug
# 5e6f1d2 HEAD@{3}: commit: Start parser workHEAD@{1} is only correct if the reset was the last thing you did. If you ran other commands first, the entry you want sits further down, so read the list instead of guessing the number.
Check before you move
Inspect the entry before you reset to it. These commands show what it contains and what history sits behind it.
git show --stat HEAD@{1}
git log --oneline -5 HEAD@{1}If the commit is not the one you want, try the next entry. Inspecting changes nothing, so you can repeat it freely.
A safer alternative
You do not have to move your current branch. Create a new branch at the lost commit instead:
git branch recovered HEAD@{1}This leaves your current state alone, and you can compare or merge recovered at your own pace. Use it when you are unsure whether the old state is what you want.
Common mistakes
- Counting wrong. Each command that moves HEAD adds an entry. Running
git statusdoes not, but a checkout, commit or second reset does. Recheckgit reflogright before you reset. - Using
HEAD~1instead ofHEAD@{1}.HEAD~1means the parent commit in the current history.HEAD@{1}means the previous position of HEAD. After a reset, they point to different places. - Waiting too long. Reflog entries expire. Recover soon after the mistake.
Exercise
You run git reset --hard HEAD~3 by mistake, then run git checkout other-branch and git switch main. Why is HEAD@{1} probably not the commit you want, and how do you find it?
Show answer
The checkout and the switch each added a reflog entry, so HEAD@{1} is now the previous move rather than the pre-reset tip. Run git reflog, find the entry just before the line "reset: moving to HEAD~3", check it with git show --stat <selector>, then run git reset --hard to that entry, or use git branch recovered <hash> to keep your current branch untouched.