Q: You ran `git push --force` to your feature branch but realized you wiped out a teammate's commit they had pushed five minutes earlier. How do you recover their commit, and how do you prevent this next time?
The commit isn't lost on the remote yet — it's still in the remote's reflog and likely in your teammate's local repo. Recovery:
#Git #Undo, Recovery & History Rewriting #L2 #Version Control #Collaboration
🎙️ Candidate Opening & Architectural Context
""Git is an immutable directed acyclic graph (DAG); knowing commands like git reflog means you never truly lose commits. The interviewer is testing: Reflog recovery, `--force-with-lease`, awareness of force-push hazards.. I structure my answer around systematic triage first, root cause analysis second, and permanent remediation third.""
Advertisement
🛠️ Production Runbook & Step-by-Step Resolution
1️⃣
Initial Diagnostics & Root Cause Analysis
The commit isn't lost on the remote yet — it's still in the remote's reflog and likely in your teammate's local repo. Recovery:
- Ask the teammate — their local branch still has the commit. They can push it back (after pulling your changes and rebasing it on top), or share the hash.
- Check the remote's reflog if you have shell access to the server (rare on managed services). On GitHub, the events API and "Network" graph sometimes still show the dangling commit hash for a while; you can fetch it directly:
git fetch origin. - Check your own clone — if you fetched their commit before force-pushing,
git reflogon the remote-tracking branch (git reflog show origin/feature-x) will list it. Cherry-pick it back:git cherry-pickthen push. - Use
git push --force-with-leaseinstead of--force. It refuses to push if the remote has commits you haven't seen — exactly this scenario.
2️⃣
Remediation & Permanent Safeguards
Prevention:
- Better:
git push --force-if-includes(Git 2.30+) — also verifies your local ref includes the latest fetch. - Branch protection rules on
main/developshould block force-push entirely. Force-push should only be allowed (and only via--force-with-lease) on personal feature branches.
💡 The Senior SRE Gold Nugget (Key Architectural Takeaway)
"Pro-Tip: Ask the teammate — their local branch still has the commit. They can push it back (after pulling your changes and rebasing it on t."
⚡ 60-Second Elevator Pitch Talking Points
- Ask the teammate — their local branch still has the commit. They can push it back (after pulling ...
- Check the remote's reflog if you have shell access to the server (rare on managed services). On G...
- Check your own clone — if you fetched their commit before force-pushing, git reflog on the remote...
Advertisement