Recovering a Corrupted Git History After a Force Push to Main
Someone ran git push --force on main and now half your team's commits have vanished from the remote. Your CI pipeline is broken, your colleagues are getting diverged-history errors, and everyone is looking at you for answers. Take a breath. Git is more forgiving than it looks in that moment.
The good news: Git does not immediately destroy commits when you force-push. It stores dangling commits in the reflog and in loose object storage for at least 30 days by default. You have a meaningful window to recover.
- How to find the lost commits using
git reflogandORIG_HEAD - How to restore your branch to the pre-force-push state
- How to safely push the recovered history back to the remote
- How to help teammates whose local branches diverged
- How to prevent this from happening again
Prerequisites
This guide assumes you are comfortable running Git from the command line and have at least read access to the repository. For the remote recovery steps, you will need push access to the main branch β or a repo admin who does. The techniques here apply to any Git host (GitHub, GitLab, Gitea, etc.).
Understanding What a Force Push Actually Does
A normal git push only succeeds if the remote tip is an ancestor of your local tip β it enforces a fast-forward. A force push skips that check and replaces the remote ref with whatever you send, regardless of what commits it overwrites.
The commits that were on the remote are now unreferenced, not deleted. They still exist as objects in the repository's object store. As long as garbage collection (git gc) hasn't run and pruned them, they are reachable through the reflog.
The reflog is per-repository and per-machine. The remote host keeps its own reflog; your local clone keeps its own. Both are useful in a recovery scenario.
Step 1 β Freeze the Damage
Before you do anything else, stop people from pulling or pushing to main. If your team continues working against the corrupted tip, recovery gets harder. A quick message in your team channel (
π€ Share this article
Sign in to saveRelated Articles
Comments (0)
No comments yet. Be the first!