Skip to content

Git Fetch Prune — Clean Remote Tracking Fix

DodaTech Updated 2026-06-24 2 min read

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 true to prune automatically on every fetch
  • Run git fetch --prune regularly in CI/CD pipelines
  • Delete remote branches after merging pull requests
  • Use git branch -a --merged to audit stale branches
  • Configure branch cleanup in your git hosting (GitHub/GitLab settings)

Common Mistakes with fetch prune

  1. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
  2. Using return to exit a function early instead of wrapping a pure value in the monad
  3. 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

### What is the difference between git remote prune and git fetch --prune?

git remote prune origin only removes stale remote tracking branches without fetching new data. git fetch --prune origin fetches updates AND removes stale branches in one operation. Use git fetch --prune to stay current with fewer commands.

Why do stale remote branches matter?

Stale branches clutter git branch -a output, can be accidentally checked out as new local branches, and make it harder to tell which branches are active. In CI/CD, stale tracking can cause deployment scripts to reference deleted branches.

How do I prune all remotes at once?

Use git fetch --all --prune to fetch updates from all remotes and prune stale tracking branches simultaneously. Or configure git config --global fetch.prune true to make every fetch operation prune automatically.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro