The Hook (The "Byte-Sized" Intro)
You ran git branch -D feature/payment and immediately regretted it. Three days of work. Gone. Except — it's not. A branch in Git is just a 41-byte pointer to a commit. Deleting the branch deletes the pointer, not the commits. The commits are still in the object database. Find the SHA, recreate the pointer, and your branch is back.
📖 What is Recovering a Deleted Branch?
Since branches are just pointers to commits, "deleting" a branch only removes the pointer. The commits remain in the repository and can be found via reflog or fsck, then a new branch can be created pointing to them.
Conceptual Clarity
What actually happens when you delete a branch:
Before delete:
feature/payment → commit F → commit E → commit D → ...
After delete:
(no pointer) commit F → commit E → commit D → ...
The commits still exist — just no branch name points to them
Recovery steps:
- Find the last commit of the deleted branch (via reflog)
- Create a new branch pointing to that commit
Real-Life Analogy
Deleting a branch is like removing a bookmark from a book. The pages are still there — you just lost the marker. Reflog helps you find which page the bookmark was on so you can put it back.
Visual Architecture
Why It Matters
- Accident recovery: Branch deletion mistakes are common, especially during cleanup.
- Cheap fix: Recreating a branch is instant — one command.
- Time-sensitive: Unreachable commits expire after ~30 days (garbage collection).
- Confidence: Knowledge of recovery removes fear of branch management.
Code
# ─── Find the deleted branch's last commit ───
git reflog
# Look for entries mentioning the branch:
# abc1234 HEAD@{5}: checkout: moving from feature/payment to main
# def5678 HEAD@{6}: commit: Add payment validation
# The SHA just before the checkout is the tip of the deleted branch
# ─── Recreate the branch ───
git branch feature/payment abc1234
# The branch is back with all its commits!
# ─── Switch to verify ───
git switch feature/payment
git log --oneline -5
# All your commits are here ✅
# ─── Alternative: if branch reflog is available ───
git reflog show feature/payment
# Shows the branch's own history
# ─── Last resort: use fsck ───
git fsck --unreachable | grep commit
# Lists dangling commits — inspect each:
git show <sha>
# Find the right one, then: git branch recovered <sha>Key Takeaways
- Deleting a branch removes the pointer, not the commits.
- Use
git reflogto find the branch's last commit SHA. - Recreate with
git branch <name> <sha>— instant recovery. - Act within 30 days before garbage collection removes unreachable commits.
Interview Prep
-
Q: Can you recover a deleted Git branch? A: Yes. Since branches are just pointers to commits, deleting a branch only removes the pointer. The commits remain in the object database. Use
git reflogto find the SHA of the branch's last commit, thengit branch <name> <sha>to recreate it. -
Q: How long do you have to recover a deleted branch? A: Approximately 30 days for unreachable commits, until Git's garbage collector removes them. The exact time depends on
gc.reflogExpireUnreachableand whengit gcruns. -
Q: What is the difference between
git branch -dandgit branch -D? A:-d(lowercase) is the safe delete — it refuses to delete a branch that hasn't been fully merged.-D(uppercase) force-deletes regardless of merge status. Both are recoverable, but-Dis riskier because the unmerged commits may be harder to find.