patterngitTip
git remote prune: clean up stale remote-tracking branches
Viewed 0 times
git pruneremote prunestale branchesfetch prunecleanup branches
Error Messages
Problem
After remote branches are deleted (e.g. after PRs merge on GitHub), local remote-tracking refs like
origin/feature/old-feature accumulate. git branch -r lists dozens of deleted branches.Solution
Prune stale remote-tracking refs:
# Preview what will be pruned (dry run)
git remote prune origin --dry-run
# Prune stale refs for origin
git remote prune origin
# Prune while fetching (most common usage)
git fetch --prune
git fetch -p
# Configure auto-prune on every fetch
git config --global fetch.prune true
# Preview what will be pruned (dry run)
git remote prune origin --dry-run
# Prune stale refs for origin
git remote prune origin
# Prune while fetching (most common usage)
git fetch --prune
git fetch -p
# Configure auto-prune on every fetch
git config --global fetch.prune true
Why
Remote-tracking branches (origin/feature/x) are local snapshots of what the remote looked like. When the remote branch is deleted, the local tracking ref is not automatically removed — it becomes stale.
Gotchas
- Pruning remote-tracking refs does NOT delete your local branches — only the
origin/refs - To also delete local branches whose upstreams are gone:
git branch -vv | grep ': gone]' | awk '{print $1}' | xargs git branch -d - Set
fetch.prune=trueglobally so you never have to think about it
Code Snippets
Pruning stale remote-tracking branches
# Fetch and prune in one command
git fetch --prune
# See which local branches track deleted remotes
git branch -vv | grep ': gone]'
# Delete those local branches
git branch -vv | grep ': gone]' | awk '{print $1}' | xargs git branch -d
# Auto-prune on every fetch
git config --global fetch.prune trueContext
After PRs are merged and remote branches are deleted on GitHub/GitLab
Revisions (0)
No revisions yet.