How to recover repository after force push
Recovering a repository after force push requires using reflog locally and coordinating with team members to restore lost commits. With over 25 years of software development experience and as the creator of CoreUI, I’ve recovered from force push incidents multiple times. Force push overwrites remote history, but local reflog and team members’ repositories retain the original commits. This approach helps restore lost work through local recovery and team coordination.
Use local reflog and team coordination to recover repository state after accidental force push.
Local recovery after force push:
# You force pushed and lost commits
git push --force origin main
# Find previous state in local reflog
git reflog
# Output shows:
# abc1234 HEAD@{0}: push --force: forced update
# def5678 HEAD@{1}: commit: Last commit before force push
# Reset to before force push
git reset --hard HEAD@{1}
# Push again (coordinate with team first!)
git push --force-with-lease origin main
Recover from team member’s repository:
# Team member still has the original commits
# On their machine:
git log --oneline origin/main
# They push their version
git push origin main:recovered-main
# You fetch and compare
git fetch origin recovered-main
git diff main origin/recovered-main
# If correct, reset to it
git reset --hard origin/recovered-main
git push --force origin main
Find lost commits:
# View reflog for remote branch
git reflog show origin/main
# Find commit before force push
# abc1234 origin/main@{0}: update by push (forced)
# def5678 origin/main@{1}: update by push
# Recover those commits
git branch recovery-branch def5678
git push origin recovery-branch
Merge recovery:
# After finding lost commits
git checkout main
git merge recovery-branch
# Or cherry-pick specific commits
git cherry-pick abc1234..def5678
Best Practice Note
Always use --force-with-lease instead of --force for safer force pushes. Communicate with team before force pushing to shared branches. Use reflog immediately after realizing the mistake. Team members’ local repositories often have the lost commits. Enable branch protection rules to prevent force push to main branches. This is how we handle force push recovery in CoreUI—quick local reflog checks, team coordination, and branch protection preventing accidental history rewrites.



