Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save ammarshah/a20c5cfc206f1aeb0f6e8db24eb5d17f to your computer and use it in GitHub Desktop.

Select an option

Save ammarshah/a20c5cfc206f1aeb0f6e8db24eb5d17f to your computer and use it in GitHub Desktop.
Git: Force Garbage Collection of Dangling Objects

Git: Force Garbage Collection of Dangling Objects

What Are Dangling Objects?

Dangling objects are commits, blobs, or trees that are no longer referenced by any branch, tag, or the reflog. They exist in Git's object database but aren't part of any reachable history.

commit A -- commit B -- commit C (HEAD)
            \
             commit B' (dangling after amending B)

How Dangling Objects Are Created?

Git creates dangling objects during normal operations when commits or blobs become unreferenced. Common scenarios include:

  • Unstaging files: When you run git restore --staged my_file, the file staged using git add my_file becomes dangling
  • Amending commits: When you run git commit --amend, the original commit becomes dangling
  • Rebasing: Old commits are replaced with new ones, leaving the originals dangling
  • Resetting branches: git reset --hard moves HEAD, leaving previous commits dangling
  • Deleting branches: Commits unique to deleted branches become dangling
  • Failed merges or rebases: Temporary objects created during these operations may be left behind

Git's Automatic Garbage Collection

Git doesn't immediately delete dangling objects. Instead, it has built-in garbage collection (GC) that runs automatically:

  • Trigger: Runs when the repository has too many loose objects (usually after ~6,700 objects)
  • Grace period: Unreachable objects are kept for a default period:
    • Recent dangling objects (< 2 weeks old): Kept in case you need to recover them
    • Old dangling objects (> 2 weeks old): Eligible for deletion during GC
  • Reflog protection: Objects referenced in the reflog are kept until the reflog entry expires (default: 90 days)

Examples of Dangling Object Types

1. Objects Git Will Eventually GC (Old Dangling Objects)

# Amend a commit from 3 weeks ago
git commit --amend
# The original commit is now dangling and > 2 weeks old
# Next automatic GC will remove it

2. Objects Git Will Preserve (Recent or Reflog-Referenced)

# Create and amend a commit today
git commit -m "Initial commit"
git commit --amend -m "Amended commit"

# The original commit is dangling but:
# - Less than 2 weeks old (grace period)
# - Still in reflog (90-day default retention)
# Git will NOT garbage collect this yet

You can view dangling objects with:

git fsck --unreachable --no-reflogs

Force Immediate Garbage Collection

To immediately remove ALL dangling objects (including recent ones and those in reflog):

# Step 1: Expire all reflog entries immediately
git reflog expire --expire-unreachable=now --all

# Step 2: Run garbage collection with immediate pruning
git gc --prune=now

What These Commands Do

  1. git reflog expire --expire-unreachable=now --all

    • Expires all unreachable reflog entries immediately
    • --expire-unreachable=now: Don't wait for the grace period
    • --all: Apply to all refs, not just the current branch
  2. git gc --prune=now

    • Runs garbage collection
    • --prune=now: Delete all loose objects that are unreachable NOW (instead of waiting for the default grace period)

Caution

This operation is permanent and irreversible.

  • Lost recovery options: You cannot recover accidentally deleted commits, amended commits, or reset branches
  • Reflog history cleared: Your safety net for undoing mistakes is removed
  • Collaborator issues: If others have references to commits you've pruned, they may encounter errors
  • No undo: There is no way to restore garbage-collected objects

When to Use This

Safe scenarios:

  • Cleaning up after large rebases in a personal repository
  • Removing sensitive data that was accidentally committed
  • Reducing repository size after confirmed unwanted history removal

Avoid when:

  • You're unsure about recent Git operations
  • Working in a shared/collaborative repository
  • You might need to recover recent changes
  • You haven't backed up important work

Alternative: Check Before Destroying

To see what would be removed without actually deleting:

# View unreachable objects
git fsck --unreachable --no-reflogs

# See reflog entries
git reflog show --all

# Estimate size savings
git count-objects -v

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment