
Recover a Deleted Git Branch in 30 Seconds Using Reflog
Quick Tip
You can recover almost any deleted Git branch or commit within 30 days by running `git reflog`, finding the lost HEAD, and checking it out into a new branch.
This post covers the fastest way to recover a deleted Git branch using the reflog—no panic, no data loss. Anyone who's accidentally deleted a branch with unmerged commits can fix the problem in roughly 30 seconds instead of rewriting code from scratch. It's a skill every developer should keep in their back pocket.
Can You Recover a Git Branch After Deleting It?
Yes—provided you act before Git's garbage collection runs (usually 30 days). When you run git branch -D feature-branch, Git removes the branch label but keeps the commits in the object database. The reflog stores every movement of HEAD, so the tip commit is still reachable. That said, if you wait too long, those dangling commits may vanish permanently.
What Is Git Reflog and How Does It Track Changes?
The reflog is a local log that records every time HEAD or a branch reference moves. Unlike git log, which shows commit history, the reflog shows actions—checkouts, resets, merges, and yes, deletions. For a full breakdown of the syntax, see Atlassian's reflog tutorial. Worth noting: the reflog lives only on your local machine (it doesn't sync to GitHub), so it won't help if you're looking for changes made solely on a teammate's laptop.
What’s the Exact Command to Restore a Deleted Branch?
Run git reflog, spot the commit hash from before the deletion, then create the branch again with git checkout -b feature-branch <hash>. Here's the thing—if the deletion happened recently (say, in the last hour), the hash is usually at the top of the output. For example:
- Run git reflog in your terminal.
- Find the line that looks like abc1234 HEAD@{2}: commit: added login form.
- Run git checkout -b feature-branch abc1234 to recreate the branch.
That's it. The branch is back—along with every commit that sat on it.
The catch? GUI tools handle this differently. SourceTree and Tower include dedicated reflog viewers that let you right-click and restore a branch. That said, many developers prefer the terminal or the official Git documentation for precision. Visual Studio Code's built-in Git panel doesn't surface reflog data by default—you'll need an extension like GitLens.
| Method | Speed | Best For |
|---|---|---|
| git reflog + checkout | ~30 seconds | Local deletions, recent history |
| GitHub/GitLab web UI | 2–5 minutes | Remote branches with open PRs |
| git fsck --lost-found | 5–10 minutes | Reflog expired or corrupted |
For a deeper dive into how Git stores objects locally, check out Pro Git's chapter on data recovery. If you're working in a team based in Asheville—or anywhere else—keep backups on GitHub or GitLab so remote copies exist even when local reflogs fail. Branch recovery doesn't have to be stressful. With the reflog, it's usually just one command away.
