What is an orphan commit?

An orphan (unreachable) commit is a commit that is no longer referenced by any branch or tag.

Common causes:

  • git reset --hard

  • git commit --amend

  • Interactive rebase

  • Deleting a branch

  • Cherry-pick/rebase experiments

Git usually keeps these commits temporarily through the reflog, allowing recovery if a mistake was made.


1. Inspect orphan commits

List unreachable objects:

git fsck --unreachable --no-reflogs

Or create .git/lost-found references:

git fsck --lost-found

Display unreachable commits nicely:

git fsck --unreachable --no-reflogs \
| grep commit \
| cut -d' ' -f3 \
| xargs git log --oneline --decorate

2. Rescue an orphan commit

If an orphan commit is still valuable:

git branch rescue <commit-id>

or

git switch -c rescue <commit-id>

Once a branch points to it, the commit is no longer orphaned.


3. Permanently delete orphan commits

Expire reflog entries:

git reflog expire --expire=now --all

Run garbage collection:

git gc --prune=now

More aggressive cleanup:

git gc --prune=now --aggressive

Git will remove unreachable commits that are no longer protected by the reflog.


Recommended workflow

# Inspect
git fsck --unreachable --no-reflogs
 
# Rescue anything important
git branch rescue <sha>
 
# Remove unreachable objects
git reflog expire --expire=now --all
git gc --prune=now

Notes

  • Git automatically performs garbage collection periodically.

  • Normally, there is no need to manually delete orphan commits.

  • During active development, especially when using interactive rebase, stacked reviews, or Gerrit, leaving orphan commits alone is safer because they provide an easy recovery path.


Best Practices

For repositories with frequent rebasing (such as stacked reviews):

  • ✅ Inspect orphan commits only when curious.

  • ✅ Rescue important commits before cleanup.

  • ✅ Let Git perform automatic garbage collection most of the time.

  • ✅ Run git gc --prune=now only when intentionally cleaning the repository or reclaiming disk space.