Accidental Force Push Data Loss
Error message
WARNING: force-pushed to main, overwriting N commits
Quick summary
A force push to a shared branch overwrote commits that others had pushed. The overwritten commits may be recoverable.
Why this happens
git push --force was run on a shared branch (e.g. main)
A rebase or amend was followed by force push to a shared branch
Branch protection was not enabled on the remote
Minimal example
git rebase main
git push --force origin main # overwrites teammates' commits!# Recovery: a teammate who has the lost commits pushes them
git push origin main # from the teammate's machine
# Prevention: use --force-with-lease
git push --force-with-lease origin main # rejected if others have pushed
# Enable branch protection on GitHub to prevent force push to mainForce push to shared branches destroys others' commits. Recover from teammates' local copies or the reflog. Prevent with --force-with-lease and branch protection.
How to diagnose
Check the remote's reflog (if accessible) for the previous HEAD
Ask teammates for their local commit hashes that were overwritten
Check GitHub's activity log or events API for the previous state
How to fix
If a teammate has the lost commits locally, they can push them again
On GitHub, the force-pushed commits may still be accessible by hash until garbage collection
Recover from the reflog: git reflog on the remote or a teammate's machine
Enable branch protection to prevent future force pushes to shared branches
How to prevent
Never force push to shared branches (main, develop)
Use --force-with-lease instead of --force
Enable branch protection rules on GitHub/GitLab
Use feature branches for history-rewriting work