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:
--force-with-lease, awareness of force-push hazards.. I structure my answer around systematic triage first, root cause analysis second, and permanent remediation third.""
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: "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."
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.
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.
- 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...
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 push force feature
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 push force feature 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 push force feature
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 push force feature Without branch protection, developers can bypass CI validation gates and push untested code directly to production branches.