Git prune helps clean your repository by removing unreachable or unused objects, but it’s often misunderstood and rarely used directly. Most real-world cleanup happens through commands like
git fetch and
git gc. In this guide, you’ll learn when to use prune safely and how it fits into Git maintenance workflows as part of a complete
Git workflow.
Quick Cheat Sheet: Git Prune Commands
| Task / Use Case | Command |
|---|---|
| Remove unreachable objects | git prune |
| Preview objects before deletion | git prune --dry-run |
| Show detailed prune output | git prune --verbose |
| Prune objects older than specific time | git prune --expire=2.weeks.ago |
| Force immediate prune | git prune --expire=now |
| Check unreachable objects | git fsck --unreachable |
| Safe cleanup workflow (recommended) | git fsck --unreachable → git prune --dry-run → git prune |
| Remove stale remote branches | git fetch --prune |
| Fetch all remotes with prune | git fetch --all --prune |
| Pull and prune remote branches | git pull --prune |
| Clean specific remote references | git remote prune origin |
| Remove stale tags while fetching | git fetch --prune --prune-tags |
| Enable auto prune for fetch | git config --global fetch.prune true |
| Disable auto prune | git config --global fetch.prune false |
| Clean repository (recommended over prune) | git gc |
| Aggressive cleanup | git gc --aggressive |
| Immediate garbage collection | git gc --prune=now |
| Compare prune vs gc behavior | git gc (preferred) vs git prune (manual) |
| Clean reflog before prune (advanced) | git reflog expire --expire=now --all |
| Full cleanup workflow (advanced) | git reflog expire --expire=now --all && git gc --prune=now |
| Check repository size | git count-objects -v |
| Simulate gc without changes | git gc --auto |
What is Git Prune? (Quick Explanation)
Git prune is a low-level command used to remove unreachable or orphaned objects from a repository. These objects are not referenced by any branch, tag, or commit and can take up unnecessary space.
What does git prune do
The git prune command deletes objects that are no longer reachable from any reference.
git pruneIt helps clean up:
- orphaned commits
- unused blobs (file data)
- detached objects
What gets deleted in git prune
Git prune removes:
- commits not referenced by any branch
- objects left behind after
git resetorgit rebase - unreachable data not tracked in current history
These objects are permanently deleted if not referenced in reflog.
Why git prune is rarely used directly
- It is a dangerous command if used without understanding
- Git automatically runs garbage collection (
git gc) - Safer alternatives like
git fetch --pruneare preferred
👉 In most cases, you should use:
git gcinstead of running git prune manually.
Clean Up Remote Branches
Use git fetch --prune to remove stale branches
git fetch --pruneThis removes references to remote branches that no longer exist.
Example:
- Branch deleted on GitHub → removed locally after fetch --prune
Use git remote prune origin
git remote prune originThis command cleans up stale references for a specific remote.
Difference between fetch prune and remote prune
| Command | Behavior |
|---|---|
git fetch --prune | Fetches updates + removes stale branches |
git remote prune origin | Only removes stale branches (no fetch) |
👉 Recommended:
- Use
git fetch --prunein daily workflow
Does Git Prune Affect Remote Repositories?
Does git prune affect remote?
No, git prune only affects your local repository. This is a common confusion. Many users think git prune removes remote branches, but remote cleanup is handled using
git remote commands instead., but that is not the case.
git prune- Removes unreachable objects locally
- Does NOT delete remote branches
- Does NOT modify remote repositories
👉 It only cleans internal Git objects in .git directory.
Why git prune does not affect remote
Git prune operates on:
- local object database
- orphaned commits and blobs
It does NOT:
- communicate with remote servers
- delete branches on GitHub/GitLab
How to clean remote branches correctly
If your goal is to clean remote references, use
git fetch with prune options.
git fetch --pruneor
git remote prune originThese commands:
- remove stale remote-tracking branches
- update local references to match remote
Difference between prune and remote cleanup
| Task | Command |
|---|---|
| Clean local unreachable objects | git prune |
| Remove stale remote branches locally | git fetch --prune |
| Clean specific remote references | git remote prune origin |
| Delete branch on remote | git push origin --delete <branch> |
❌ Many users think:
git prune origin👉 This is incorrect usage.
✔ Correct approach:
git remote prune originRemove Unreachable Objects
What are unreachable objects in Git
Unreachable objects are:
- commits not linked to any branch
- blobs or trees not referenced
- data left after reset or rebase
Use git prune safely with --dry-run
Always preview before deleting:
git prune --dry-runThis shows what will be removed without actually deleting anything.
Use git fsck before pruning
Check for unreachable objects:
git fsck --unreachableThis helps identify objects before running prune and inspecting history using
git log.
Example cleanup workflow
git fsck --unreachable
git prune --dry-run
git pruneFor aggressive cleanup:
git gc --prune=nowgit fetch --prune vs git pull --prune
What git fetch --prune does
git fetch --prune- Fetches latest changes
- Removes stale remote-tracking branches
- Does NOT modify working directory
What git pull --prune does
git pull --prune- Fetches updates
- Removes stale branches
- Merges changes into current branch
When to use each command
- Use fetch --prune → safe, preferred for daily usage
- Use pull --prune → when you want to update and merge together using
git pull
👉 Best practice:
git fetch --prune
git pullThis gives more control over updates.
Git Prune vs Git GC
Difference between git prune and git gc
Understanding the difference between git prune and git gc is essential for safe repository cleanup.
| Command | Purpose |
|---|---|
git prune | Removes unreachable objects manually |
git gc | Performs full garbage collection (recommended) |
git pruneworks on low-level objectsgit gchandles cleanup automatically and safely
👉 In most cases, prefer using
git gc for safe cleanup.
git gcWhen to use git gc instead of prune
Use git gc when:
- You want safe cleanup of repository
- You are unsure about unreachable objects
- You want Git to manage optimization automatically
git gcFor deeper cleanup:
git gc --aggressivegit gc --prune=now explained
git gc --prune=now- Immediately removes unreachable objects
- Skips default expiration period
- Useful for freeing disk space quickly
Safe vs Dangerous Git Prune Usage
Is git prune safe
- Safe when used with
--dry-run - Safe in local repositories for cleanup
- Not safe if you are unsure about data references
git prune --dry-runWhy git prune can delete data permanently
- Deletes objects not referenced by any branch
- Removes data that cannot be recovered, especially after history changes using
git reset. - Ignores objects once reflog expires
Best practices before running prune
Follow these steps:
git fsck --unreachable
git prune --dry-run
git pruneAdditional safety:
- Take backup if needed
- Avoid running on shared repositories
- Prefer
git gcwhen unsure
Automate Pruning in Git
Enable auto prune with git config
git config --global fetch.prune trueThis automatically removes stale branches during fetch.
Use fetch.prune true
With this setting:
git fetchbehaves likegit fetch --prune- Stale remote references are cleaned automatically
Clean repositories automatically
Combine with garbage collection:
git gc --autoThis allows Git to:
- detect when cleanup is needed
- perform maintenance automatically
Frequently Asked Questions
1. What does git prune do?
git prune removes unreachable objects such as orphaned commits, blobs, and trees that are no longer referenced by any branch.2. Is git prune safe to use?
git prune can be risky because it permanently deletes data. It is safer to use git prune with --dry-run or use git gc for automatic cleanup.3. What is git fetch --prune?
git fetch --prune removes stale remote-tracking branches that no longer exist on the remote repository.4. What is the difference between git prune and git gc?
git prune removes unreachable objects manually, while git gc performs automatic garbage collection and is the recommended approach.5. When should I use git prune?
Use git prune only when you need to manually remove unreachable objects and understand the risks. Otherwise, use git gc.6. What does git pull --prune do?
git pull --prune fetches changes from the remote repository, removes stale branches, and merges updates into the current branch.7. How to preview objects before pruning in Git?
Use git prune --dry-run to see which objects will be removed without actually deleting them.8. What is git gc --prune=now?
git gc --prune=now performs immediate garbage collection and removes unreachable objects without waiting for expiration.9. Can git prune delete commits permanently?
Yes, git prune can permanently delete unreachable commits, especially after reflog expiration.10. How to automatically prune branches in Git?
Enable automatic pruning using git config --global fetch.prune true to clean stale branches during fetch operations.Summary
Git prune is a low-level cleanup tool used to remove unreachable objects, but it is rarely needed directly. Most cleanup tasks should be handled using safer commands like
git fetch and
git gc.
For daily workflows, use fetch prune to clean remote branches. For repository maintenance, rely on git gc. Use git prune only when you fully understand its impact.



![Git Error: Cannot Delete Branch Checked Out or Used by Worktree [SOLVED]](/cannot-delete-branch-checked-out-at/git-cannot-delete-branch_hu_3f86bd25a59d627d.webp)






