How to recover a deleted branch using reflog.

•

•

The Reflog Remembers What You Deleted

I remember sitting in my home office three years ago, the only light coming from a dual-monitor setup and a dying desk lamp, feeling that specific, cold knot of dread in my stomach. I had just run a command that I thought was cleaning up some stale feature branches, but instead, I had accidentally nuked the one containing two weeks of critical refactoring. There’s a particular kind of silence that follows a catastrophic mistake in the terminal—a quiet that makes you wonder if you should just close the laptop and pretend the last hour never happened. I spent forty minutes spiraling before I realized that learning how to recover a deleted branch isn’t about magic; it’s about knowing where Git hides its digital breadcrumbs.

I’m not going to give you a lecture on Git internals or suggest you install some heavy-duty GUI tool to “save” your workflow. I’ve made this mistake too many times to justify any fluff. Instead, I’m going to show you the exact, no-nonsense commands I use to hunt down lost commits using the reflog. We’ll focus on the practical reality of clawing back your work without the panic, because once you understand the underlying mechanics, a deleted branch is just a minor inconvenience rather than a career-ending event.

Table of Contents

Why I Thought My Work Was Gone Forever

Why I Thought My Work Was Gone Forever

It was one of those Friday afternoons where you feel invincible because the deployment went smoothly. I was cleaning up some local clutter, trying to be “tidy,” and I ran a command that I thought was targeting a stale feature branch. Instead, I watched my terminal confirm the deletion of the main work branch for the upcoming sprint. The immediate physical reaction is always the same: a cold sink in your stomach and a sudden, intense desire to close your laptop and pretend the internet doesn’t exist.

I did the standard panic check first. I ran `git branch` expecting to see it there, but it was gone. I tried to `git checkout` the branch name, only to be met with that familiar, mocking error message: pathspec ‘feature/payment-gateway-refactor’ did not match any file(s) known to git. At that moment, I was convinced I had just wiped out three days of complex logic. I wasn’t just looking at a minor mistake; I was looking at a massive hole in my productivity and a very awkward conversation with my lead on Monday. I realized then that I didn’t just need a quick fix; I needed a way to recover lost git commits before the local garbage collection decided to make my mistake permanent.

The First Git Emergency Recovery Steps You Need

The First Git Emergency Recovery Steps You Need

The first thing you need to do is stop typing. I know, it sounds counterintuitive, but when the panic sets in, the instinct is to start running random commands to “fix” things, which is exactly how you end up making a bad situation permanent. Before you try to force any complex rebase or a massive reset, you need to realize that Git is actually quite good at being a historian. Even if you think you’ve nuked the branch, the underlying commits are likely still sitting in your local database, just temporarily unattached to any name.

Your primary lifeline here is the git reflog command usage. Think of the reflog as the black box flight recorder for your local repository; it tracks every single time your `HEAD` moved, regardless of whether a branch name was attached to it. I usually run `git reflog` immediately to see a chronological list of where I’ve been. Once you spot the hash of the commit right before the deletion happened, you can effectively undo git branch deletion by simply pointing a new branch name at that specific SHA. It’s much cleaner than trying to hunt through the garbage collector’s debris.

Lessons from the wreckage: How to avoid a total meltdown next time

  • Get comfortable with `git reflog`. It is effectively your black box flight recorder. Even when you delete a branch, Git doesn’t immediately purge the commits; it just loses the pointer. The reflog tracks every time your `HEAD` moves, so as long as you haven’t run a heavy garbage collection, your “lost” work is likely still sitting there, just unindexed.
  • Stop using `git reset –hard` as a way to “clean up” unless you are absolutely certain. I used to do this constantly when I was junior, thinking I was tidying up my workspace, but all I was doing was throwing away uncommitted state that I actually needed. If you’re unsure, use `git stash` or create a temporary backup branch first.
  • Treat your local branches like temporary, but treat your remote tracking branches like truth. If you delete a local branch by mistake, check `git branch -r` immediately. If you pushed that branch to the origin at any point in the last few days, the server is your safety net. It’s much easier to fetch a remote branch than to reconstruct a local one from a pile of orphaned commits.
  • Commit early, even if the code is garbage. I know, it feels “unprofessional” to push half-baked logic, but a commit is a permanent marker in the Git timeline. If I have a commit hash, I can recover the work. If I only have uncommitted changes in my working directory when the power goes out or the terminal crashes, I am genuinely screwed.
  • Learn to read the output of `git fsck –lost-found`. It’s not pretty, and it’s definitely not the “elegant” way to work, but when the reflog fails you, this is the nuclear option. It digs through the actual database objects to find dangling commits. It’s tedious, but I’ve used it to salvage entire afternoons of work when I thought I’d nuked my local repository entirely.

The things I wish I'd known before I hit enter

Git is much harder to break than it feels in the moment; the data is almost always still sitting in your local reflog, even if the branch pointer is gone.

Panic is the enemy of recovery—the more commands you run in a frenzy, the higher the chance you actually overwrite something useful.

The best way to handle a mistake is to treat it like a production incident: stop, verify what’s actually lost, and use the simplest tool available to pull it back.

The Lesson in the Logs

The Lesson in the Logs: Git reflog.

At the end of the day, recovering a branch usually comes down to one thing: knowing that `git reflog` is your best friend when the history looks like a crime scene. I learned the hard way that as long as you committed the work locally, the data is still sitting there in your object database, just waiting for a pointer to find it again. You don’t need to panic, and you definitely don’t need to re-clone the repo. Just find that SHA, check the diff to make sure it’s the right one, and re-attach a branch name to it. It’s a simple mechanical process, but it’s one that requires a calm head and a refusal to let the initial spike of adrenaline dictate your terminal commands.

If you’re currently staring at a terminal feeling like you’ve just deleted a week of your life, take a breath. We have all been there, and honestly, these are the moments that actually turn you into a senior engineer. You can read about Git internals in a textbook, but you don’t truly understand how the plumbing works until you’ve had to manually reconstruct it after a mistake. Don’t let the mistake haunt you; just write down what happened, fix the workflow that allowed it to happen, and get back to the code. The branch is back, the lesson is learned, and the production environment is still standing.

Frequently Asked Questions

What happens if I've already run a garbage collection (git gc) after deleting the branch?

This is where the panic usually sets in. If you’ve run `git gc`, you’ve essentially told Git to go through the basement and throw out everything that isn’t currently being used. Since that branch was deleted, its commits are now “unreachable.”

Can I recover a branch if I accidentally deleted the local version but it's still sitting on the remote server?

The short answer is yes, and this is actually the best-case scenario. If the remote still has it, you haven’t actually lost anything; you’ve just lost your local pointer to it. I’ve done this more times than I’d like to admit while trying to clean up my local workspace. Just run `git fetch`, find the branch name, and `git checkout` it again. Git will see it on the origin and rebuild your local copy perfectly.

How do I know which commit hash in the reflog actually corresponds to the tip of my lost branch?

The trick is looking for the “moving” marker. When you run `git reflog`, you aren’t just seeing a list of commits; you’re seeing a timeline of where your HEAD has been. Look for the entry right before the mistake happened—usually labeled `checkout: moving from [branch] to [something else]` or `commit: [your message]`. The hash immediately following the last known “good” action is your target. If you’re unsure, check the timestamp; the one right before the panic is usually the winner.

Is there any way to prevent this kind of panic in the future without just telling everyone "don't delete things"?

Telling people “don’t delete things” is a losing battle; it’s like telling a cyclist to avoid hills. Instead, I’ve started leaning on branch protection rules in GitHub or GitLab. It’s a blunt instrument, but it stops the accidental nuking of main or develop. For everything else, I just remind the team that `git reflog` is our safety net. It doesn’t stop the mistake, but it stops the panic.

Tomasz Wierzbicki

About Tomasz Wierzbicki

I am not interested in arguing about frameworks. I am interested in why the thing broke at two in the morning and what would have prevented it. Most of what I know came from being wrong in production and writing it down afterwards.