Recover lost workUsually recoverable

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.

quick fix
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

  1. 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 1
    git branch feature-x a1b2c3d
    
  2. If 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 2
    git reflog
    
    # or search directly for the branch name:
    git reflog | grep feature-x
    
    Show the PowerShell (Windows) version
    PowerShell (Windows)
    git reflog | Select-String 'feature-x'
    
  3. 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 3
    git branch feature-x a1b2c3d
    git switch feature-x
    git log --oneline -5
    
  4. If 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 4
    git 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 reflog

The undo history for everything - the single most important recovery command.

git branch

List, create, rename and delete branches.

git fsck

Scan the object database for orphaned commits and blobs the reflog no longer lists.

git fetch

Download 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

← All Git Rescue guides