git recover deleted branch — using reflog
Quick Answer
# Find the commit the deleted branch was pointing to
git reflog
# Recreate the branch at that commit
git checkout -b recovered-branch abc1234
When to use this
You accidentally deleted a branch with git branch -D and want to get it back.
Other causes & fixes
Reading the reflog output
Look for a line like abc1234 HEAD@{3}: checkout: moving from recovered-branch to main — the hash before the checkout is your branch tip.
git reflog --oneline | head -20
Recover a branch deleted on the remote
If the branch was deleted on GitHub/GitLab but still exists locally:
git push origin recovered-branch
Reflog only keeps entries for ~90 days
If the branch was deleted a long time ago, the commit may have been garbage-collected. Act quickly.
git gc --prune=now # do NOT run this if you are trying to recover
Related