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.
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:
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:
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
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 --orphanis 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.
Related reading
- How to have 'git log' show filenames like 'svn log -v
- How to ignore certain files in Git
- How to import a GIT non-Eclipse Java project into Eclipse?
- How to import existing Git repository into another?
- How to install a specific Chart version
- How to install an npm package from GitHub directly
- How to install git on a docker ubuntu image?
- How to install Python package from GitHub?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseInterview Questions practice on Codemia
Over 11,000 interview practice questions for 300+ companies, searchable by company and role.