ShareYourGroupShareYourGroupGit Help

Recover a branch I deleted

I ran git branch -D and the branch had work that was not merged.

The problem

I ran git branch -D and the branch had work that was not merged.

How the practice repository looked before

$ git status --short --branch
## main
$ git log --oneline -5
1f118d4 First

The fix

$ git reflog
1f118d4 HEAD@{0}: checkout: moving from feature to main
7a7441f HEAD@{1}: commit: Work on feature
1f118d4 HEAD@{2}: checkout: moving from main to feature
1f118d4 HEAD@{3}: commit (initial): First
$ git branch feature HEAD@{1}
(no output: that is normal)

Check the result

$ git branch
  feature
* main
$ git log --oneline feature
7a7441f Work on feature
1f118d4 First

Why this works

Deleting a branch only removes its name. The commits stay in the repository until they are cleaned up. The reflog shows where HEAD was just before you left that branch, and git branch <name> <place> makes a new name for it.

If this is not quite your case

If the reflog does not show the branch work, git fsck --lost-found can list commits that no branch points at. After you recover the branch, push it again if it was on a remote; deleting a local branch does not delete the remote one.

This is an independent guide. It is not connected with the Git project, GitHub, GitLab or any company named here. Every command below was run on 14 November 2026 with the real program (git version 2.56.0.windows.4) in a new empty practice repository; the output shown is what it printed. Facts about options were read in the official Git documentation on 14 November 2026.