Git
Branch Recovery
Code Management
Version Control
Data Loss Prevention

Can I recover a branch after its deletion in Git?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Yes, a deleted Git branch can often be recovered because deleting a branch usually removes only the reference, not the commits themselves. Recovery is easiest when the branch was deleted recently and Git has not yet garbage-collected the unreachable commits. The main job is to find the commit where the branch used to point and then recreate the branch name.

Start with reflog

The fastest recovery tool is usually git reflog. Reflog records where HEAD and other references have pointed recently, including checkouts and branch movements.

bash
git reflog

Look for entries that mention moving from the deleted branch or commits that clearly belong to it. Once you find the commit hash, recreate the branch:

bash
git branch recovered-feature 1a2b3c4d
git switch recovered-feature

If you remember the original branch name, you can recreate it with that exact name instead.

Recover After a Local Branch Deletion

Suppose you ran git branch -D feature-x by mistake. If you had checked out that branch recently, reflog usually still knows its tip.

Typical recovery flow:

bash
git reflog
git branch feature-x 1a2b3c4d
git switch feature-x

This works because the commit objects still exist even though the branch reference is gone.

Check for a Remote Copy

If the branch was pushed before deletion, recovery may be even simpler. A remote-tracking reference or the remote branch itself may still exist.

bash
git branch -r
git switch -c feature-x origin/feature-x

If the remote branch was also deleted, you may still find the relevant commit in another clone, in CI logs, or in pull request metadata. Git makes commits hard to lose immediately, but easier to lose after time and cleanup operations.

Use git fsck as a Fallback

If reflog does not show the commit, git fsck --lost-found can help locate dangling commits.

bash
git fsck --lost-found

This command reports unreachable objects. You can inspect a dangling commit with:

bash
git show 1a2b3c4d

If the commit is the one you want, recreate the branch:

bash
git branch feature-x 1a2b3c4d

This is less convenient than reflog because you may need to inspect several commits manually, but it can still save work after an accidental deletion.

Understand the Time Limit

Branch recovery is easiest before Git prunes unreachable objects. Reflog entries expire, and garbage collection can eventually remove commits that are no longer reachable from any reference.

That means:

  • recover sooner rather than later
  • avoid aggressive cleanup until the branch is restored
  • if the branch matters, recreate the reference as soon as you find the tip commit

The longer you wait, the more likely the recovery path narrows.

Verify the Recovered History

After recreating the branch, inspect the commit graph to confirm you recovered the correct tip and not an older intermediate commit.

bash
git log --oneline --decorate --graph --all

This is especially important if the branch had rebases, force-pushes, or several similar commit messages.

Protect Important Branches

Git recovery is helpful, but prevention is better. Good habits include:

  • pushing important feature branches before risky history edits
  • using protected branches on the remote for shared work
  • avoiding -D unless you are sure the branch is no longer needed

Those practices reduce the odds that recovery becomes urgent in the first place.

Common Pitfalls

  • Assuming a deleted branch means the commits are gone immediately.
  • Waiting too long and letting reflog expiration or garbage collection remove the recovery path.
  • Recreating the branch at the wrong commit because the first matching hash looked familiar.
  • Forgetting to check whether the branch still exists on the remote.
  • Running cleanup commands before verifying that the needed commit has been restored.

Summary

  • Deleting a Git branch usually removes a reference, not the underlying commits.
  • 'git reflog is the first and best recovery tool in most cases.'
  • If reflog is insufficient, check remotes and then try git fsck --lost-found.
  • Recreate the branch as soon as you identify the correct commit.
  • Recover quickly, because unreachable commits do not stay around forever.

Course illustration
Course illustration

All Rights Reserved.