Git
Version Control
Prune
Git Fetch
Git Pull

Automatic prune with Git fetch or pull

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Git keeps remote-tracking references such as origin/feature-x in your local repository. When a branch is deleted on the remote, those references do not disappear automatically unless you prune them, so branch lists can slowly fill with stale entries.

What prune Actually Removes

git fetch --prune removes remote-tracking refs that no longer exist on the remote server. It does not delete your local branches, and it does not rewrite commit history.

That distinction matters. If origin/old-branch disappears after pruning, only the remote-tracking reference is gone. A local branch named old-branch remains until you delete it yourself.

This is why pruning is generally safe as part of normal maintenance. It is a cleanup step for references, not a destructive operation on your worktree.

One-Time Use: git fetch --prune

The most direct command is:

bash
git fetch --prune origin

You can also omit the remote name if your default remote setup is straightforward:

bash
git fetch --prune

After that, commands like git branch -r and tools that list remote branches will stop showing branches that were deleted upstream.

If you want pruning and tag updates together, pair the command with your normal fetch options rather than building a separate cleanup ritual.

Make Pruning Automatic for Fetch

If you want this behavior every time you fetch, set the repository or global configuration.

bash
git config fetch.prune true

For all repositories on your machine:

bash
git config --global fetch.prune true

With that setting in place, plain git fetch behaves like git fetch --prune.

You can also scope the setting to one remote:

bash
git config remote.origin.prune true

That is useful when you want pruning for origin but not for every remote.

What About git pull?

git pull is essentially fetch followed by an integration step such as merge or rebase. Because the fetch phase is still there, pruning can be enabled for pull in the same practical way.

The simplest route is still to enable fetch.prune globally or in the repository. Then every git pull benefits because its internal fetch is pruned too.

You can also run:

bash
git pull --prune

That works, but many teams prefer configuring prune once rather than relying on everyone to remember an extra flag.

Why Automatic Pruning Helps

The immediate benefit is cleaner branch discovery. Stale origin/... refs make it harder to tell which branches still exist and can confuse developers who are trying to pick the right source branch for a fix or review.

Automatic pruning also reduces subtle mistakes in scripts and tooling. If a script assumes that every remote-tracking branch shown locally still exists upstream, stale refs can create misleading results.

For long-lived repositories with lots of feature branches, auto-prune is a low-cost way to keep local metadata honest.

Pruning does not remove local branches that used to track deleted remote branches. To find those, look for branches whose upstream is marked as gone.

bash
git branch -vv

If you see a branch you no longer need, delete it explicitly:

bash
git branch -d old-branch

Use -D only when you intentionally want to force deletion.

This separation is important because remote-tracking cleanup and local branch deletion are different decisions.

Common Pitfalls

Confusing remote-tracking refs with local branches is the most common misunderstanding. prune cleans the former, not the latter.

Assuming prune is dangerous because it “deletes branches” is also inaccurate. It deletes stale references to remote branches that are already gone upstream.

Running git remote prune origin without understanding it is another source of confusion. That command also removes stale remote-tracking refs, but git fetch --prune is usually better because it updates refs and prunes in one step.

Finally, enabling prune does not solve every branch hygiene problem. Local branches still need deliberate cleanup.

Summary

  • 'git fetch --prune removes stale remote-tracking refs'
  • it does not delete your local branches or commits
  • 'git config fetch.prune true is the simplest way to make pruning automatic'
  • 'git pull benefits from the same setting because pull performs a fetch first'
  • use git branch -vv to inspect local branches whose upstream remote branch is gone

Course illustration
Course illustration

All Rights Reserved.