Code or Script
Undo a git reset --hard and get your commits back
Committed work survives a hard reset for as long as the objects stay in the repository (default: at least 30 days before garbage collection). The commit is unreachable, not deleted. Uncommitted changes are the genuinely lost case.
Stop doing this first
- Do not run git gc, git prune, or 'git clean -fdx'.
- Do not delete or re-clone the repository directory.
- Do not force-push over the branch until you have the commits back.
The safe order of operations
- 1
Do not run git gc, git prune or git repack — those are the only commands that truly destroy your objects.
- 2
Read git reflog and find the SHA from before the reset.
- 3
Create a new branch at that SHA instead of resetting again, so you never lose your current state.
- 4
If reflog is empty (fresh clone, or reset on another machine), use git fsck --lost-found to list dangling commits.
- 5
Cherry-pick or merge what you need back onto your working branch.
Commands that do the work
Find the pre-reset state
git reflog --date=iso | head -40Rescue it onto a branch
git branch rescue/lost-work HEAD@{1}List dangling commits when reflog is gone
git fsck --full --no-reflogs --lost-foundInspect a dangling commit before restoring
git show --stat <sha>Realistic odds
Likely recoverable for committed work; uncommitted changes are usually lost unless your editor kept local history.
Your case will differ in the details. Load this scenario into the recovery form and add what you already tried — you'll get a plan for your exact situation.
Get my recovery plan