Recover a Deleted Git Branch
Problem
You accidentally deleted a branch that wasn't fully merged, and now you need to get it back.
git branch -d feature-branch
# error: the branch 'feature-branch' is not fully merged
# hint: If you are sure you want to delete it, run 'git branch -D feature-branch'
git branch -D feature-branch
# Deleted branch feature-branch (was 1a98de2).
Solution
Git keeps unreferenced commits in its object database. When you delete a branch, Git shows the commit hash it was pointing to: (was 1a98de2).
Use that hash to recreate the branch:
git branch feature-branch 1a98de2
Your branch is now restored with all its commits intact.
If You Missed the Hash
Check the reflog to find recent branch tip changes:
git reflog
Look for entries like checkout: moving from feature-branch to main - the hash at the start of that line is what you need.
Or search for your branch specifically:
git reflog | grep feature-branch
Notes
- Git retains unreferenced commits for ~90 days by default before garbage collection
- The reflog only tracks local activity, so this won't help with branches deleted on other machines
- If
git gchas run and removed the commit, recovery is not possible
See Also
git reflog- View history of HEAD changesgit fsck --lost-found- Find dangling commits