git
version control
orphan commit
delete commit
git tips

How to hard delete an orphan commit in Git

Codemia Knowledge Hub ·

An orphaned Git commit is usually an unreachable commit object: it is still in the object database, but no branch or tag points to it. To delete it for real, make sure no refs or reflog entries still protect it, then force garbage collection to prune it.

Confirm that the commit is unreachable

Before removing anything, check that no branch, tag, or reflog entry still references the commit.

bash
git fsck --unreachable --no-reflogs
git reflog --all

If the hash still appears in a reflog, Git may keep the commit even when no branch points to it. That is deliberate: reflogs are a recovery safety net.

Remove remaining references

If a branch or tag still points to the commit, delete it first:

bash
git branch -D old-branch
git tag -d old-tag

When the commit was orphaned by a reset or rebase, the obstacle is usually the reflog, not a visible branch.

Expire reflogs and prune now

Once you are sure the commit should go, expire unreachable reflog entries and run garbage collection immediately:

bash
git reflog expire --expire-unreachable=now --all
git gc --prune=now

The first command removes reflog protection for unreachable objects. The second prunes the commit along with its unreachable tree and blob objects.

Check the result

bash
git fsck --unreachable --no-reflogs

If the commit no longer appears, Git has pruned it from the local object database. For extra confidence, run git cat-file -t <hash>: once the object is gone, it fails instead of printing commit.

Why git gc alone often leaves the commit

Running plain git gc frequently does not remove the commit because:

  • Reflogs may still reference it
  • Default prune settings are conservative
  • Recently unreachable objects are kept on purpose for recovery

So "orphaned" and "prunable right now" are not the same thing.

Remotes and other clones keep their copy

These commands only touch your local object database. If the commit exists on a remote or another developer's machine, pruning locally does not erase it there. Removing published history is a separate job that usually starts with deleting or rewriting refs on the remote. Object reachability is per clone until refs are coordinated across repositories.

Mistakes to avoid

  • Pruning before verifying reachability can destroy a commit you still need.
  • Ignoring reflogs lets the commit survive after you delete visible branches.
  • Local garbage collection does not delete the commit from the remote.
  • An orphan branch created with git checkout --orphan is not the same as an unreachable commit, and needs different cleanup.

If you are unsure, clone or bundle the repository first so you keep a recovery path.


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Interview Questions practice on Codemia

Over 11,000 interview practice questions for 300+ companies, searchable by company and role.

Browse interview questions