This Fixes: I Deleted the Wrong Branch — Recovering It From the Reflog

You ran git branch -D against the wrong name, the terminal printed Deleted branch, and your stomach dropped. The work is almost certainly still there.

Git is bad at throwing things away, and in a crisis that is a feature. When you delete a branch, all you actually remove is a pointer — a forty-character file under .git/refs/heads/ — and the small log of where that pointer has been. The commits themselves, the real tree of your work, sit untouched in the object database until garbage collection eventually decides nothing references them. That leaves you a recovery window measured in weeks, not seconds. This is how to use it, on git 2.45 or anything recent.

Before anything else, stop garbage collecting

The one command that can turn “recoverable” into “gone” is git gc. Do not run it, do not run git prune, and above all do not run git gc --prune=now to “tidy up” while you panic. Everyday commands are safe — a normal git fetch --prune only touches remote-tracking refs, not your objects — but a manual prune with an aggressive expiry can delete the exact commits you are trying to save.

Also check the easy exit first. If the branch was ever pushed, the remote still has it, and the local delete changed nothing there. In that case the recovery is not a recovery at all:

# If it was ever pushed, the remote still holds it
git fetch origin
git switch -c feature/pricing-api origin/feature/pricing-api

From here on I will assume the harder case: a purely local branch that never left your machine.

The common case: find it in the reflog

When you delete a branch, Git removes that branch’s own reflog — but not HEAD’s. Every checkout, commit, merge and reset you make is recorded in .git/logs/HEAD, and that log still points at the tip of the branch you deleted, as of the last time you had it checked out. git reflog reads exactly that log. It is the same tool that gets you out of a botched interactive rebase, pointed at a different kind of accident.

# Show HEAD's history with dates, most recent first
git reflog --date=iso

# Narrow it to the branch you lost
git reflog --date=iso | grep pricing-api

# You are looking for the line where you last left the branch, e.g.:
# a1b2c3d HEAD@{4}: checkout: moving from feature/pricing-api to main

# The SHA on that line is the branch tip. Recreate the branch there:
git branch feature/pricing-api a1b2c3d

The entry you want is usually the checkout: moving from <your-branch> to ... line — the SHA on the left is the commit the branch pointed at when you switched away from it. Recreating the branch with git branch <name> <sha> puts the pointer back exactly where it was. If instead you check the SHA out directly with git switch <sha>, you land in detached HEAD, which is fine for a look but is not a branch until you name it. Prefer recreating the branch outright.

When the reflog can’t help: git fsck

The reflog only knows about commits this clone has actually touched. If the branch tip was fetched but never checked out, or the reflog entry has already aged out, the HEAD log will not have it. The fallback is to scan the object store directly for commits that nothing points to.

# List unreachable commits and write them to .git/lost-found/
# --no-reflogs treats reflog-only commits as dangling too
git fsck --full --no-reflogs --unreachable --lost-found

# Output includes lines like:
# dangling commit a1b2c3d4e5f6...

# Inspect each candidate before you trust it
git show --stat a1b2c3d
git log --oneline -5 a1b2c3d

# When you have found the right tip, recreate the branch
git branch recovered/pricing-api a1b2c3d

--lost-found writes every dangling object into .git/lost-found/ — commits under commit/, everything else under other/ — so you have files to grep even if the terminal output scrolls away. Expect noise: an active repository accumulates dangling commits from amends, rebases and resets. Commit dates and git show --stat almost always disambiguate the one you want from the rest.

How long you actually have

The reassuring “90 days” figure is real but it is not the whole picture, and for a deleted branch it is the wrong half. gc.reflogExpire defaults to 90 days, but that governs entries still reachable from a ref tip. The commits of a deleted branch are, by definition, no longer reachable — and those are governed by gc.reflogExpireUnreachable, which defaults to 30 days. On top of that, even once an object is fully unreferenced, git prune only removes it if it is older than gc.pruneExpire, which defaults to two weeks.

The practical reading: a deleted local branch is comfortably recoverable for a few weeks, often longer, and only actually disappears when garbage collection runs against expired, unreachable objects. If your team churns through short-lived branches and you want a wider safety net, widen the windows explicitly:

# Keep reflog entries much longer, globally
git config --global gc.reflogExpire "200 days"
git config --global gc.reflogExpireUnreachable "90 days"

That is cheap insurance. Reflog entries are tiny, and the objects they keep alive are already on disk.

The lesson is not “be careful with -D“. You will mistype a branch name eventually; everyone does. The lesson is that a deleted branch is a recovery exercise, not a loss — right up until the moment you try to “clean things up” with git gc --prune=now. Find the SHA first. The garbage collector can wait.

Free interactive tool

Website compliance checklist

What your site has to do, based on what it actually does

Answer as much or as little as you like — the list builds as you go. Nothing is stored against your name and no email is required.

Most technology problems are not technology problems. They are control problems.

The systems exist. The investment has been made. The question is whether leadership can understand, direct, evidence, and sustain what those systems produce. Find out where control exists — and where it only appears to.