Git Alias to Prune Stale Remote Tracking Branches

Create a git alias that removes local remote-tracking references for branches that no longer exist on the remote server.

Cleanup Aliases

Detailed Explanation

Prune Remote Tracking Branches

Over time, your local repository accumulates references to remote branches that have been deleted on the server. The prune alias cleans these up:

[alias]
    prune = fetch --prune

What Are Stale Remote Tracking Branches?

When you clone a repository, git creates local references like origin/feature-xyz that track remote branches. When someone deletes feature-xyz on the remote (after merging a PR, for example), your local origin/feature-xyz reference persists. Over months, you can accumulate dozens of these stale references.

What fetch --prune Does

  1. Contacts the remote (origin) and fetches new data
  2. Identifies local remote-tracking references that no longer exist on the remote
  3. Deletes those stale references

Before and After

# Before pruning
git branch -r
# origin/main
# origin/feature-login    (deleted on remote)
# origin/feature-payments  (deleted on remote)
# origin/feature-search
# origin/fix-typo          (deleted on remote)

git prune

# After pruning
git branch -r
# origin/main
# origin/feature-search

Auto-Prune on Every Fetch

If you want pruning to happen automatically on every fetch:

git config --global fetch.prune true

With this global setting, every git fetch and git pull automatically prunes stale references.

Pruning Specific Remotes

If you work with multiple remotes, you can prune a specific one:

git fetch --prune upstream

This is useful in fork-based workflows where you have both origin (your fork) and upstream (the original repository).

Use Case

Run this alias weekly or after a batch of pull requests are merged. It is essential for keeping your branch list clean and avoiding confusion when tab-completing branch names. In repositories with many contributors, stale references accumulate quickly.

Try It — Git Alias Builder

Open full tool