Need help recovering repository after accidental/malicious force push wiped all branches #184630
Replies: 6 comments
-
|
Since a "force push" does not immediately delete the old commits from GitHub's servers, yes, it is often possible to recover the repository, even if all branches were overwritten. Here are the steps to recover your code: Option 1: The "Local Reflog"If you (or a collaborator) have a local clone of the repository that was pulled before the disaster occurred, the history is still on that machine.
git reset --hard <COMMIT_HASH>
git push --force origin <branch_name>
Option 2: Find the "Before" Commit ID via GitHub APIIf you don't have a local copy, you can find the commit hash (SHA) of your branch from before the force push using GitHub's public events API.
# Create a new branch pointing specifically to that recovered commit
git checkout -b recovered-branch <BEFORE_HASH>
# Push this branch to GitHub to save it
git push origin recovered-branch
Important: Act quickly. While GitHub keeps these "dangling" commits for a while, they are eventually cleaned up by automated garbage collection. |
Beta Was this translation helpful? Give feedback.
-
|
GitHub can't restore it automatically (no backups for force pushes). On your local machine (if you still have it): Run git reflog, find old commit hash before wipe, then git checkout -b recover and push it back. Prevention: Enable branch protection to block force pushes. |
Beta Was this translation helpful? Give feedback.
-
|
🕒 Discussion Activity Reminder 🕒 This Discussion has been labeled as dormant by an automated system for having no activity in the last 60 days. Please consider one the following actions: 1️⃣ Close as Out of Date: If the topic is no longer relevant, close the Discussion as 2️⃣ Provide More Information: Share additional details or context — or let the community know if you've found a solution on your own. 3️⃣ Mark a Reply as Answer: If your question has been answered by a reply, mark the most helpful reply as the solution. Note: This dormant notification will only apply to Discussions with the Thank you for helping bring this Discussion to a resolution! 💬 |
Beta Was this translation helpful? Give feedback.
-
|
This is painful, but don’t panic — recovery is often still possible.
If anyone has a local copy: git reflog You can find old commits and push them back.
If you find an old commit: git reset --hard GitHub may be able to restore deleted history if contacted quickly. 👉 Open a support ticket and include: Repo name Time matters here — the sooner you act, the better 👍 |
Beta Was this translation helpful? Give feedback.
-
|
This happens more often than you’d think — and recovery is usually possible.
If anyone has a local repo: git reflog Find the old commit and push it back: git push --force origin GitHub can sometimes restore history, but only for a limited time. Include: Repo name Act fast — recovery chances drop over time 👍 |
Beta Was this translation helpful? Give feedback.
-
🚨 Recovering a Repository After a Destructive Force PushYes — recovery is often still possible. A force push does not immediately delete commits, it only moves branch references. The underlying data usually remains for some time. Below is a complete recovery guide with strategies ranked by effectiveness. 🥇 1. Check Local Clones (Best Chance)If you or a collaborator has a local copy from before the force push:
git reflog Find the last good commit, then: git checkout -b recovery <COMMIT_HASH> 🧠 2. Use
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Select Topic Area
Question
Body
Hi, a repository was force-pushed, and all branches were overwritten with a single file. This effectively wiped out the entire codebase. Is it possible for GitHub to restore the repository state from before the destructive force push?
Beta Was this translation helpful? Give feedback.
All reactions