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

  1. Run git reflog to see recent HEAD movements
  2. Find the line showing the checkout or commit before the branch was deleted
  3. Note the commit hash (e.g., a1b2c3d)
  4. Recreate the branch: git checkout -b recovered-branch a1b2c3d
  5. 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