How to Recover a Deleted Git Branch
You deleted a branch. Maybe it had unmerged work. Maybe you ran git branch -D thinking you were done, then realized you were not. Git almost certainly still has your commits — here is how to get them back.
The fast path: git reflog
Git's reflog is a local log of every position HEAD has been in. Even after a branch is deleted, the commits that were on it remain in the object store for 30–90 days. The reflog tells you where to find them.
git reflogYou'll see output like this:
abc1234 HEAD@{0}: checkout: moving from feature/auth to main
def5678 HEAD@{1}: commit: add OAuth provider support
9ab0123 HEAD@{2}: commit: wire up login endpoint
...Find the last commit that was on your deleted branch — usually the most recent entry before you switched away. Then restore it:
git checkout -b feature/auth def5678 # recreate the branch at that commitIf you are not sure which commit to restore, create the branch at each candidate and check git log --oneline to confirm the history looks right.
If you know the branch name but not the commit
Use git reflog with a grep to narrow it down:
git reflog | grep "feature/auth"This shows every reflog entry that mentions your branch name — including checkouts onto it, commits made while on it, and the moment it was deleted.
Recovering a branch that was pushed to remote
If the branch existed on a remote before it was deleted locally, it may still exist there:
git fetch origin # update remote refs
git checkout -b feature/auth origin/feature/authIf the remote branch was also deleted, check if any teammate still has it locally. Their reflog has a copy of the commit history. A git push from their machine restores it.
Last resort: git fsck
If the reflog does not help (e.g. you cleared it or the commit is very old), git fsck lists all unreachable objects:
git fsck --lost-found
# Look for "dangling commit" entries
git show <dangling-commit-hash> # inspect each one
git checkout -b recovered <hash> # restore if it's the right oneCommits only survive until Git's garbage collector runs. The default window is 30 days for unreachable commits, 90 days for those referenced by the reflog. If git gc ran recently, data may be gone permanently.
Git Unfucked documents remote branch disasters, multi-author recovery, reflog deep dives, and force-push undos — all in one reference you can search offline when you're mid-panic.
Get Git Unfucked →