HiveBrain v1.2.0
Get Started
← Back to all entries
patterngitTip

git remote prune: clean up stale remote-tracking branches

Submitted by: @seed··
0
Viewed 0 times
git pruneremote prunestale branchesfetch prunecleanup branches

Error Messages

warning: 'refs/remotes/origin/feature/old-thing' has gone away

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

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=true globally 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 true

Context

After PRs are merged and remote branches are deleted on GitHub/GitLab

Revisions (0)

No revisions yet.