shellstreak

How to undo git reset --hard

Updated · Every command on this page ran in our practice shell

Short answer

Run git reflog, find the entry from just before the reset (often HEAD@{1}), check git status, and run git reset --keep HEAD@{1} (or git branch rescue HEAD@{1} to keep it on a new branch). This recovers committed work only: uncommitted changes wiped by reset --hard can't be recovered by git.

You ran git reset --hard and your latest commits disappeared from git log. If they were committed, they're almost certainly still there: git keeps a log of everywhere HEAD has been, called the reflog. If the changes were never committed, the honest answer is that git can't bring them back. The examples below ran in a practice shell, on a repository where someone just ran git reset --hard HEAD~2.

Find the old commit in the reflog

git reflog lists where HEAD pointed, newest first. The top line is the reset. The line under it, HEAD@{1}, is where you were just before. Each entry shows the commit's hash too.

example
learner@shellstreak:~/recipes$ git reflog
4df7d66 (HEAD -> main) HEAD@{0}: reset: moving to HEAD~2
7a76ad2 HEAD@{1}: commit: Add muffin recipe
ba602fb HEAD@{2}: commit: Add scone recipe
4df7d66 (HEAD -> main) HEAD@{3}: commit (initial): Add bread recipe

Reset back to it, safely

Run git status first: if anything is uncommitted, commit or stash it. Bookmark where you are with git branch before-recovery, so the recovery itself can be undone. Then move the branch with git reset --keep, using the reflog name or the hash. Unlike --hard, --keep refuses to overwrite files with local edits.

example
learner@shellstreak:~/recipes$ git status && git branch before-recovery && git reset --keep HEAD@{1} && git log --oneline
On branch main
nothing to commit, working tree clean
7a76ad2 (HEAD -> main) Add muffin recipe
ba602fb Add scone recipe
4df7d66 (before-recovery) Add bread recipe

Or keep it on a new branch

If you're not sure, don't move your branch at all: git branch rescue HEAD@{1} puts a new branch on the lost commit. You can look at it, merge it, or cherry-pick from it at your own pace.

example
learner@shellstreak:~/recipes$ git branch rescue HEAD@{1} && git log --oneline rescue
7a76ad2 (rescue) Add muffin recipe
ba602fb Add scone recipe
4df7d66 (HEAD -> main) Add bread recipe

What each reset mode keeps

All three modes move the branch. They differ in what happens to the changes from the commits you step back over:

  • --soft: the changes stay staged, ready to recommit.
  • --mixed (the default): the changes stay in your files, unstaged.
  • --hard: the changes are discarded, files and all.
example
learner@shellstreak:~/recipes$ git reset --soft HEAD~1 && git status -s
A  muffins.txt

Mixed reset: back to your files

With the default mode, files the commits added become untracked again and edits become unstaged. Nothing in your folder changes, so this is the gentle way to un-commit and regroup.

example
learner@shellstreak:~/recipes$ git reset HEAD~2 && git status -s
?? muffins.txt
?? scones.txt

What the reflog can't recover

git reset --hard also overwrites uncommitted edits in your files. Those were never stored in a commit, so there is no reflog entry for them and git can't restore them. (Only changes you had staged with git add leave a trace, and digging them out is a last resort.) Your editor's undo history or local history is the only hope there.

example
learner@shellstreak:~/recipes$ git status -s && git reset --hard && git status -s
 M bread.txt
HEAD is now at 7a76ad2 Add muffin recipe

Before a risky reset: stash or commit

Make anything you care about recoverable first. git stash shelves uncommitted edits, so a hard reset can't touch them; a commit on a branch is even safer. Then reset.

example
learner@shellstreak:~/recipes$ git stash && git reset --hard HEAD~1 && git stash list
Saved working directory and index state WIP on main: 7a76ad2 Add muffin recipe
HEAD is now at ba602fb Add scone recipe
stash@{0}: WIP on main: 7a76ad2 Add muffin recipe

Check yourself

One real exercise from the “Git history & undo” unit. No account needed.

Someone ran git reset --hard HEAD~2 and the scone and muffin commits disappeared. Check git status first, then put main back where it was before that reset with git reset --keep.

What's checked: the files recipes/scones.txt, recipes/muffins.txt; the repository's commits.

Common mistakes

  • Assuming everything is gone and deleting the repository. Committed work survives a reset; check git reflog first.

  • Running more commands before recovering. Each one adds reflog entries, so HEAD@{1} moves; read the reflog again and use the hash if unsure.

  • Using git reset --hard to unstage a file. git restore --staged file does that without touching your edits.

  • Resetting a branch others have pulled. Use git revert for shared commits.

  • Expecting the reflog on a fresh clone to know your old commits. It's local to the copy where you made them.

Questions

Can I recover uncommitted changes after git reset --hard?+

Usually not. Uncommitted edits aren't in any commit, so the reflog doesn't have them. Check your editor's undo or local history. Files you had staged may still exist as loose objects, but recovering them is fiddly and not guaranteed.

How long does the reflog keep entries?+

By default, real git keeps reflog entries for about 90 days (30 for commits that are no longer on any branch) before `git gc` may remove them. Recover soon rather than later.

What does HEAD@{1} mean?+

Where HEAD pointed one move ago. `HEAD@{2}` is two moves ago, and so on. Unlike `HEAD~1` (the parent commit), it follows your actions, including resets and switches.

Reading is step one. Now make it stick.

Practise this in a five-minute lesson with instant feedback. No account needed.

Practise it now