🔀 Deleted ≠ Lost
You deleted a branch, or force-pushed over one, and realized seconds later it had unmerged work. Panic sets in — but Git almost never actually loses your commits. It just loses the pointer to them.
🐞 The Problem
git branch -D feature/important-work # Deleted branch feature/important-work (was a1b2c3d). # ...and you needed that.
🔍 Why This Is Recoverable (Cause & Effect)
Deleting a branch only removes the label pointing at a commit. The commit object itself stays in Git's internal database until garbage collection runs (which is infrequent, usually 30+ days by default). => The data still exists -- you just need to find its commit hash again.
🔢 Step 1 — Find the Lost Commit With reflog
git reflog
# a1b2c3d HEAD@{2}: commit: Add payment retry logic
# f4e5d6c HEAD@{3}: checkout: moving from main to feature/important-work
# ...
# reflog shows every place HEAD has pointed, including branches you deleted
🔢 Step 2 — Recreate the Branch From That Commit
git branch feature/important-work a1b2c3d git checkout feature/important-work # Your branch is back, exactly as it was before deletion
📌 Recovering From a Bad Force Push (Same Trick)
# You force-pushed and overwrote remote history git reflog show origin/main # Or, if you still have the old local reflog before the force push: git reflog # Find the commit hash BEFORE the bad force push, then: git push origin a1b2c3d:main --force # Restores the remote branch to its previous state
📌 If the Branch Was Deleted on GitHub/GitLab (Never Pulled Locally)
# GitHub keeps deleted branches recoverable via the "restore branch" # button on the PR page for a limited time. If that's gone: git fsck --no-reflogs --unreachable --lost-found # Lists "dangling" commit objects still in the repo's object database
🛡️ Prevent This Going Forward
# Never force push without --force-with-lease -- it refuses to overwrite # work you haven't seen yet, unlike plain --force: git push --force-with-lease origin feature/important-work
reflog is local and defaults to a 90-day expiry — meaning almost every “I deleted my branch” panic is fully recoverable if you act before running garbage collection manually.
