You accidentally deleted an important branch that was never merged and no one has a local copy. How do you recover it?
The Answer: Recover the commit first, then recreate the branch pointer. Most of the time, the data is still there. ✅
When a branch is deleted, Git usually removes only the reference (the branch name), not the commit objects immediately. Recovery depends on where the commits still exist.
Fast recovery flow (best to worst):
-
Check local reflog (if your clone had ever seen the branch):
git reflog --all --date=iso | grep "<branch-name-or-commit-hint>"If you find the commit SHA:
git checkout -b <recovered-branch> <sha> -
Search dangling commits in the repo object database:
git fsck --lost-found --no-reflogsInspect candidate commits:
git show <sha>Then restore:
git checkout -b <recovered-branch> <sha> -
Check remote hosting refs/history:
- If the branch had an open PR, the PR often still points to the head commit.
- CI logs, deployment metadata, or old release notes may contain the commit SHA.
-
If GitHub branch was deleted and no one has SHA:
- Check closed/open PRs, workflow logs, and commit search in the repository.
- As last resort, contact GitHub Support quickly (garbage collection can eventually prune unreachable objects).
Important: Once you recover the SHA, immediately recreate and push:
git checkout -b <recovered-branch> <sha>
git push -u origin <recovered-branch>
Prevention tips:
- Protect critical branches with deletion restrictions.
- Require PRs for long-running work.
- Keep backups/mirrors for high-risk repos.
- Turn on branch protection + status checks.
In short: branch name gone ≠ work gone. Find the commit SHA, recreate the branch, push it, and protect important branches going forward.
