Git
Version Control
Branch Recovery
Git Commands
Undo Delete

Git undo local branch delete

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

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.

bash
git reflog

Look for the commit that belonged to the deleted branch. Once you find it, recreate the branch:

bash
git branch feature-x a1b2c3d

Or switch to it immediately:

bash
git switch -c feature-x a1b2c3d

That is the normal recovery workflow.

Example Flow

Suppose you deleted the branch:

bash
git branch -D feature-x

Then you realize you still needed it.

Find the old tip:

bash
git reflog

Sample result might show:

text
a1b2c3d HEAD@{3}: checkout: moving from feature-x to main

Then restore it:

bash
git branch feature-x a1b2c3d

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.

bash
git switch -c feature-x --track origin/feature-x

That recreates the local branch from the remote-tracking branch.

So before digging into reflog, ask whether the branch still exists remotely:

bash
git branch -r

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.

bash
git fsck --lost-found

This can show dangling commits:

text
dangling commit a1b2c3d...

Inspect one:

bash
git show a1b2c3d

If it is the right commit, recreate the branch:

bash
git branch feature-x a1b2c3d

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 -d instead of -D when possible
  • inspect with git log --oneline --graph --all before 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 -D habitually 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 reflog is the first recovery tool to use.'
  • If the remote branch still exists, recreate the local branch from origin/....
  • 'git fsck --lost-found is a useful fallback when reflog is not enough.'
  • Recovery is easier when you act quickly before unreachable commits are cleaned up.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.