Git undo local branch delete
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Deleting a local Git branch removes the branch reference, not the commits themselves, so recovery is often easy if you act before garbage collection. The main task is to find the commit where the branch pointed and recreate the branch name from that commit.
Recover with reflog
The best first tool is usually git reflog, because it records reference movements and recent HEAD positions.
Look for the commit that belonged to the deleted branch. Once you find it, recreate the branch:
Or switch to it immediately:
That is the normal recovery workflow.
Example Flow
Suppose you deleted the branch:
Then you realize you still needed it.
Find the old tip:
Sample result might show:
Then restore it:
The branch name is back, and the commits are reachable again.
If the Branch Existed on a Remote
If the local branch was deleted but the remote branch still exists, recovery is even simpler.
That recreates the local branch from the remote-tracking branch.
So before digging into reflog, ask whether the branch still exists remotely:
If it does, use the remote as the source of truth.
If reflog Is Not Enough
If you cannot find the branch tip in reflog, Git may still know about the commit through unreachable-object inspection.
This can show dangling commits:
Inspect one:
If it is the right commit, recreate the branch:
This is a fallback path, not the first tool to reach for.
What -d Versus -D Changes
git branch -d name refuses to delete the branch if Git thinks it is not fully merged. -D forces deletion.
That affects safety, but not the general recovery principle. In both cases, deleting the branch usually removes only the reference, not the commit objects immediately.
Recovery is easier when the deletion was recent and no cleanup has run.
Recovery Window Matters
Git can eventually garbage-collect unreachable commits. So while branch deletion is usually reversible in the short term, it should not be treated as permanent safety.
If the branch was deleted long ago and no other references point to its commits, recovery may become harder or impossible.
That is why reflog is most effective right after the mistake.
Practical Safety Habits
A few habits reduce branch-loss panic:
- push important branches before cleanup
- use
git branch -dinstead of-Dwhen possible - inspect with
git log --oneline --graph --allbefore aggressive cleanup - avoid deleting branches during rebases or unfinished recovery work
The goal is not to avoid branch deletion forever. It is to avoid deleting references you still need.
Common Pitfalls
- Assuming branch deletion instantly destroys the commits.
- Forgetting to check whether the remote branch still exists.
- Using
git branch -Dhabitually when normal safe deletion would do. - Looking only at branch names instead of recovering from commit hashes.
- Waiting too long and making unreachable commits harder to recover.
Summary
- Undoing a local branch delete usually means recreating the branch at its old tip commit.
- '
git reflogis the first recovery tool to use.' - If the remote branch still exists, recreate the local branch from
origin/.... - '
git fsck --lost-foundis a useful fallback when reflog is not enough.' - Recovery is easier when you act quickly before unreachable commits are cleaned up.
Related reading
- git update-index --assume-unchanged and git reset
- Git update submodules recursively
- Git vs Mercurial vs SVN
- Git What's the best practice to git clone into an existing folder?
- git, whitespace errors, squelching and autocrlf, the definitive answers
- Git with large files
- Git workflow and rebase vs merge questions
- Git workflow and rebase vs merge questions
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.