Q: A junior engineer ran `git reset --hard HEAD~5` on their local branch and lost five commits of in-progress work. Nothing was pushed. Walk me through the recovery.
A --hard reset moves the branch pointer and discards working-tree changes — but the commits themselves are not deleted. They're orphaned ...
🛠️ Production Runbook & Step-by-Step Resolution
Production Solution & Architecture
A --hard reset moves the branch pointer and discards working-tree changes — but the commits themselves are not deleted. They're orphaned (no branch points to them) and will sit in the object database until garbage collection runs (default: ~30 days for unreachable objects, 90 days if reachable from reflog). Recovery via the reflog, which is a per-clone log of every move HEAD (and each branch ref) has made: You'll see something like: def5678 is the tip of the work that was wiped. Restore it: Two important caveats: (1) the reflog is local — it doesn't help if the commits never existed on this clone (e.g. teammate's machine). (2) Run recovery *before* git gc runs. If you suspect a teammate already ran git gc --prune=now, the objects may genuinely be gone. As a habit, alias reset --hard to require confirmation, and teach the team git stash and git switch -c backup before risky operations. --- ## 🟡 Collaboration & Remote Workflows
git reflog # shows HEAD movements
# or, more targeted:
git reflog show feature-branch # shows that branch's history
- Immediate Triage: A --hard reset moves the branch pointer and discards working-tree changes — but the commits the
- Run targeted verification commands before modifying configuration.
- Automate permanent guardrails (CI check, alerts, IaC policy) to prevent recurrence.