Git Fetch Prune — Clean Remote Tracking Fix
In this tutorial, you'll learn about Git Fetch Prune. We cover key concepts, practical examples, and best practices.
Git keeps remote tracking branches (origin/feature-branch) even after the remote branch is deleted. Over time, stale references accumulate, cluttering git branch -a and causing confusion about which branches still exist remotely.
The Problem
git branch -a
Shows:
* main
remotes/origin/feature-login
remotes/origin/feature-profile
remotes/origin/old-experiment # Deleted on remote
remotes/origin/stale-branch # Deleted on remote
The remote branches old-experiment and stale-branch were deleted on the remote but still appear locally.
Wrong Approach
# WRONG — manually deleting each stale branch
git branch -dr origin/old-experiment
git branch -dr origin/stale-branch
Right Approach
# Fetch and prune stale remote tracking branches
git fetch --prune origin
Expected output:
- [deleted] (none) -> origin/old-experiment
- [deleted] (none) -> origin/stale-branch
Step-by-Step Fix
Step 1: See what stale branches exist
# Dry run — shows what would be deleted
git remote prune origin --dry-run
Step 2: Prune stale branches
git remote prune origin
Step 3: Fetch with prune (combines both operations)
git fetch --prune origin
Step 4: Configure automatic pruning
git config remote.origin.prune true
Now every git fetch or git pull automatically prunes.
Step 5: Prune all remotes
git fetch --all --prune
Step 6: Clean up merged local branches
# Delete local branches that have been merged
git branch --merged | grep -v "^\*\|main\|master" | xargs -r git branch -d
Step 7: Verify clean branch list
git branch -a
Prevention Tips
- Set
git config --global fetch.prune trueto prune automatically on every fetch - Run
git fetch --pruneregularly in CI/CD pipelines - Delete remote branches after merging pull requests
- Use
git branch -a --mergedto audit stale branches - Configure branch cleanup in your git hosting (GitHub/GitLab settings)
Common Mistakes with fetch prune
- Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
- Using
returnto exit a function early instead of wrapping a pure value in the monad - Mixing let bindings with <- bindings in do notation, producing type errors
These mistakes appear frequently in real-world GIT code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro