Recover a branch you deleted by mistake
I deleted a Git branch — how do I get it back?
Short answer
Deleting a branch removes a pointer, not the commits. Find the branch's last commit hash in the reflog and recreate the branch at that hash with git branch <name> <hash>. If the branch was pushed, the copy on the server may still exist and can simply be fetched back.
git reflog
git branch recovered-branch a1b2c3d
Recreate the branch at the commit it pointed to before deletion
Does this match your situation?
- You ran git branch -D and immediately regretted it.
- A branch is missing from git branch after cleaning up merged branches.
- Git printed 'Deleted branch feature-x (was a1b2c3d)' and you need that work back.
Step-by-step fix
Look for the hash Git printed when it deleted the branch
Git tells you the tip commit on the way out: 'Deleted branch feature-x (was a1b2c3d)'. If that line is still in your terminal scrollback, you already have everything you need and can skip to step 3.
step 1git branch feature-x a1b2c3dIf the message is gone, search the reflog
The reflog records every commit HEAD has visited, so the tip of your deleted branch is in there from the last time you had it checked out. Filtering by the branch name finds it quickly.
step 2git reflog # or search directly for the branch name: git reflog | grep feature-xShow the PowerShell (Windows) version
PowerShell (Windows)git reflog | Select-String 'feature-x'Recreate the branch at that commit
Creating a branch at a hash restores it completely — the commit carries the full history behind it, so the entire branch comes back, not just the tip.
step 3git branch feature-x a1b2c3d git switch feature-x git log --oneline -5If it was pushed, just fetch it back
A branch deleted locally but still present on the server is not lost at all. Check what the remote still has and recreate your local branch tracking it.
step 4git fetch origin git branch -r | grep feature-x git switch -c feature-x origin/feature-x
Why this works
Branches in Git are extraordinarily cheap because a branch is literally a file containing one 40-character commit hash. Deleting it removes that file and nothing else — every commit it referenced remains in the object database, still linked to its parents. Recreating the branch just writes the hash back. This is also why git branch -d refuses to delete unmerged branches without -D: it's the only warning Git gives you, since the deletion itself is trivially reversible only as long as you can find the hash.
If that didn’t work
- Run git fsck --lost-found and look at the dangling commits it reports.
- Inspect candidates with git log --oneline <hash> to find the right branch tip.
- Check GitHub/GitLab — the branch may still be listed under deleted branches or recoverable from a closed pull request.
How to stop it happening again
- Use git branch -d (lowercase) so Git refuses to delete anything unmerged.
- Push work-in-progress branches to the remote — a second copy costs nothing.
- Before a cleanup sweep, run git branch --no-merged to see exactly what still has unique work.
Commands used in this guide
git reflogThe undo history for everything - the single most important recovery command.
git branchList, create, rename and delete branches.
git fsckScan the object database for orphaned commits and blobs the reflog no longer lists.
git fetchDownload new commits from the remote without changing any of your files.
Still stuck?
Search the full command reference and every other rescue guide — there are 56 of them, covering everything from detached HEAD to force-push disasters.
Browse all guides →Frequently asked questions
Does deleting a Git branch delete the commits?
No. It removes a reference — a small file holding one commit hash. The commits stay in the object database until garbage collection removes unreachable objects, which is normally weeks later, so recreating the branch at the old hash restores everything.
How do I find the commit hash of a deleted branch?
git reflog lists everywhere HEAD has been, including the tip of the branch from when you last had it checked out. If that fails, git fsck --lost-found reports dangling commits, and git log --oneline on each candidate identifies the right one.
Can I recover a branch deleted on GitHub?
Often, yes. If the branch had an associated pull request, GitHub offers a Restore branch button on the closed PR. Otherwise, anyone who has fetched the branch still has the commits locally and can push it back up.
Related rescue guides
I ran git reset --hard and lost my commits — how do I undo it?
Committed work is almost never really gone. git reflog lists every position HEAD has held, including the one just before your reset, so you can jump straight back to it with git reset --hard HEAD@{1}. Uncommitted changes that the reset wiped are a different story — those are only recoverable if they had been staged at some point.
Read the fix →Usually recoverableMy rebase went wrong and my commits are gone — how do I get them back?
The pre-rebase commits are still in the object database. git reflog shows the position from before the rebase started, and ORIG_HEAD is a shortcut to it — git reset --hard ORIG_HEAD usually restores everything in one command.
Read the fix →Usually recoverableSomeone force-pushed and my commits are gone — how do I get them back?
Your local clone still has the commits, and your reflog records where the branch pointed before the force-push overwrote it. Find that hash with git reflog, recover it to a branch, and push it back. If your local copy is already clean, a teammate's clone or the server's reflog may still have it.
Read the fix →Always fixableI committed to main instead of my feature branch — how do I move the commits?
Create a branch at your current position to keep the commits, then reset the branch you polluted back to where it should be. Nothing is lost because the new branch holds the commits the whole time.
Read the fix →