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.
Want to master this scenario in a live sandbox? KodeKloud's Advanced Git Workflows, Rebase & Interactive Diagnostics covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Direct Answer: "During a major release branch cut, we encountered this exact scenario and used Git internals to recover cleanly. The interviewer is testing: Deep understanding of refs, reflog, and Git's garbage collection model.. I structure my answer around systematic triage first, root cause analysis second, and permanent remediation third."
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.
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.
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
-
Running raw git push --force rather than git push --force-with-lease on shared team feature branches when configuring junior reset hard
git push --forceunconditionally overwrites remote commits made by teammates, whereas--force-with-leaseaborts if the remote ref was updated. - Resolving complex merge conflicts with blind checkout overwrite strategies without running unit tests when configuring junior reset hard Unilateral conflict resolution frequently clobbers complementary logic introduced in parallel feature branches, creating regression bugs.
-
Discarding corrupted working directories prematurely instead of recovering lost state using git reflog when configuring junior reset hard
git reflogtracks every HEAD movement, allowing instant recovery of discarded commits, botched rebases, or deleted local branches. - Disabling branch protection rules and mandatory PR approval reviews on shared release branches when configuring junior reset hard Without branch protection, developers can bypass CI validation gates and push untested code directly to production branches.