I deleted my branch
What just happened
Deleting a branch only deletes a label pointing to a commit. The commits themselves stay in your local repository for weeks (until garbage collection), and the reflog remembers where the branch was.
Potential risks
- Running git gc --prune=now would really erase the orphan commits.
- If the branch was only on a teammate's machine, only they can restore it.
Before you run anything
- Do not run git gc or git prune until you have recovered the branch.
- Deleted on GitHub? The PR page has a โRestore branchโ button.
Recovery steps
- 1
Find the last commit of the branch
git branch -D printed โDeleted branch x (was abc1234)โ. Lost that message? The reflog lists every position HEAD went through.
Safe$ git reflog --date=relative | grep -i 'my-branch' - 2
Recreate the branch on that commit
Replace abc1234 with the hash you found. Nothing is overwritten: you only create a new label.
Safe$ git branch my-branch abc1234 - 3
Push it back to the remote
Only if the branch also disappeared from the remote.
Caution$ git push -u origin my-branch
These procedures are provided for information only. Read each command and adapt it to your situation before running it. Disclaimer