How to recover a deleted branch in Git
· Category: Git
Short answer
Use git reflog to find the commit hash where the deleted branch pointed, then recreate it with git checkout -b branch-name commit-hash. For more Git recovery techniques, see how to amend a Git commit.
Steps
- Run
git reflogto see recent HEAD movements - Find the line showing the checkout or commit before the branch was deleted
- Note the commit hash (e.g.,
a1b2c3d) - Recreate the branch:
git checkout -b recovered-branch a1b2c3d - Push it:
git push -u origin recovered-branch
Tips
- Reflog entries expire after about 90 days by default, so recover promptly
- Always push important branches to a remote for backup
- See how to cherry-pick a commit for selective recovery