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:
git reflog
# Entry for the deleted branch is gone
git branch -a
# Branch no longer listedThe 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 -Dwas run weeks or months ago- Commits from the branch are not reachable from any branch or tag
git logdoes 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=nowran, 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 -Don the wrong branch
Step-by-Step Fix
- 1.Find unreachable commits using
git fsck: - 2.```bash
- 3.git fsck --lost-found --no-reflogs
- 4.# Lists dangling commits and blobs
- 5.# Dangling commits are tip commits of orphaned branches
- 6.
` - 7.Examine dangling commits to find the ones from your lost branch:
- 8.```bash
- 9.git fsck --lost-found --no-reflogs 2>&1 | grep "dangling commit" | awk '{print $3}' | while read hash; do
- 10.echo "=== $hash ==="
- 11.git log --oneline -5 $hash
- 12.echo ""
- 13.done
- 14.
` - 15.Restore the branch once you find the correct commit:
- 16.```bash
- 17.git branch recovered-branch <commit-hash>
- 18.git log --oneline recovered-branch
- 19.
` - 20.Search the lost-found directory:
- 21.```bash
- 22.ls .git/lost-found/commit/
- 23.# Each file is a dangling commit hash
- 24.for f in .git/lost-found/commit/*; do
- 25.echo "=== $(cat $f) ==="
- 26.git log --oneline -3 $(cat $f)
- 27.done
- 28.
` - 29.If garbage collection already ran, recovery is extremely difficult. Check:
- 30.- Other developers' clones may still have the branch
- 31.- Remote backups (GitHub, GitLab) may have the commits
- 32.- CI/CD build artifacts may contain the code
- 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 bundleto create offline backups of important branches - Never run
git gc --prune=nowunless you are certain no recovery is needed - Set up a backup remote that retains all refs indefinitely
- Use
git noteto annotate commits with context before branch deletion - Document branch deletion procedures including backup steps in your team workflow