Introduction

Git's reflog tracks branch movements and HEAD changes, enabling recovery of deleted branches. However, reflog entries expire after a configurable time (default 90 days for reachable commits, 30 days for unreachable ones). When entries expire, orphaned branches become difficult to find:

bash
git reflog
# Entry for the deleted branch is gone
git branch -a
# Branch no longer listed

The commits still exist in the object database until garbage collection runs, but finding them requires deeper Git tools.

Symptoms

  • Deleted branch cannot be found in reflog
  • git branch -D was run weeks or months ago
  • Commits from the branch are not reachable from any branch or tag
  • git log does not show the orphaned commits
  • Garbage collection has not yet run (commits still exist)

Common Causes

  • Branch deleted long ago and reflog entries expired
  • git gc --prune=now ran, cleaning up unreachable objects
  • Developer switched machines and the reflog does not exist on the new machine
  • Repository was cloned without reflog history
  • Accidental git branch -D on the wrong branch

Step-by-Step Fix

  1. 1.Find unreachable commits using git fsck:
  2. 2.```bash
  3. 3.git fsck --lost-found --no-reflogs
  4. 4.# Lists dangling commits and blobs
  5. 5.# Dangling commits are tip commits of orphaned branches
  6. 6.`
  7. 7.Examine dangling commits to find the ones from your lost branch:
  8. 8.```bash
  9. 9.git fsck --lost-found --no-reflogs 2>&1 | grep "dangling commit" | awk '{print $3}' | while read hash; do
  10. 10.echo "=== $hash ==="
  11. 11.git log --oneline -5 $hash
  12. 12.echo ""
  13. 13.done
  14. 14.`
  15. 15.Restore the branch once you find the correct commit:
  16. 16.```bash
  17. 17.git branch recovered-branch <commit-hash>
  18. 18.git log --oneline recovered-branch
  19. 19.`
  20. 20.Search the lost-found directory:
  21. 21.```bash
  22. 22.ls .git/lost-found/commit/
  23. 23.# Each file is a dangling commit hash
  24. 24.for f in .git/lost-found/commit/*; do
  25. 25.echo "=== $(cat $f) ==="
  26. 26.git log --oneline -3 $(cat $f)
  27. 27.done
  28. 28.`
  29. 29.If garbage collection already ran, recovery is extremely difficult. Check:
  30. 30.- Other developers' clones may still have the branch
  31. 31.- Remote backups (GitHub, GitLab) may have the commits
  32. 32.- CI/CD build artifacts may contain the code
  33. 33.- IDE local history may have the files

Prevention

  • Increase reflog expiration time:
  • ```bash
  • git config --global gc.reflogExpireUnreachable 180
  • `
  • Tag important commits before deleting branches: git tag save-point abc1234
  • Push branches to a remote before deleting them locally
  • Use git bundle to create offline backups of important branches
  • Never run git gc --prune=now unless you are certain no recovery is needed
  • Set up a backup remote that retains all refs indefinitely
  • Use git note to annotate commits with context before branch deletion
  • Document branch deletion procedures including backup steps in your team workflow