git
git fetch
git prune
version control
software development

What does 'git fetch -p' or --prune mean

Master System Design with Codemia

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

Introduction

git fetch --prune tells Git to remove stale remote-tracking references that no longer exist on the remote. It does not delete your local branches, and it does not garbage-collect Git objects. It simply keeps your local view of remote branches accurate after a fetch.

What Gets Pruned

When you fetch from a remote such as origin, Git updates references like:

  • 'origin/main'
  • 'origin/feature-x'
  • 'origin/release-2025'

These are remote-tracking branches. They are local references that mirror what Git last saw on the remote.

If someone deletes feature-x on the remote, your local repository does not automatically remove origin/feature-x the next time you run a plain git fetch. That stale reference remains until you prune it.

Example:

bash
git fetch origin --prune

After that, Git removes remote-tracking refs for branches that no longer exist on origin.

What It Does Not Delete

This is the part people often worry about unnecessarily. --prune does not delete:

  • your local branches such as feature-x
  • your commits
  • unmerged work in your working tree

So if you have a local branch named feature-x, pruning origin/feature-x does not destroy the local branch. It only removes the stale remote-tracking reference.

You can see the difference clearly:

bash
git branch -a

This command shows both local branches and remote-tracking branches. After pruning, the deleted remote branch disappears from the remotes/origin/... section, but your local branches stay unless you delete them yourself.

Why This Is Useful

Without pruning, your repository can keep showing branches that no longer exist on the remote. That leads to confusion such as:

  • thinking a remote branch still exists when it was deleted days ago
  • rebasing or comparing against stale refs
  • clutter in branch listings and tooling

git fetch --prune keeps your local metadata aligned with reality.

--prune Versus git prune

These are different commands.

  • 'git fetch --prune cleans up stale remote-tracking refs'
  • 'git prune is low-level repository cleanup for unreachable objects'

They are not interchangeable.

That distinction matters because people often see the word "prune" and assume it means deep repository cleanup. In the fetch context, it only concerns remote-tracking references.

Example Workflow

Suppose your team deletes merged feature branches on the server regularly. A useful update workflow might be:

bash
git fetch origin --prune
git branch -r

Now the remote branch list reflects what still exists.

If you also want to remove a local branch that used to track a deleted remote branch, you must do that explicitly:

bash
git branch -d feature-x

That is a separate decision from pruning the remote-tracking ref.

Make It the Default

If you like this behavior every time you fetch, configure it once:

bash
git config --global fetch.prune true

Then ordinary git fetch behaves as though --prune was supplied.

You can also enable it per remote if you want finer control.

Relation to git remote prune

You may also see:

bash
git remote prune origin

That command prunes stale remote-tracking refs for origin without performing a fresh fetch first. In many day-to-day workflows, git fetch --prune is more convenient because it updates refs and prunes in one step.

Common Pitfalls

The most common mistake is thinking --prune deletes local branches. It does not. It removes stale remote-tracking refs only.

Another mistake is confusing git fetch --prune with git prune, which is a different low-level maintenance command.

Developers also often leave stale origin/... refs around for months and then rely on them accidentally. Pruning regularly prevents that drift.

Finally, remember that a pruned remote-tracking branch may still correspond to a perfectly valid local branch. If you want to delete that local branch too, do it explicitly.

Summary

  • 'git fetch --prune removes stale remote-tracking branches after fetching.'
  • It keeps your local view of the remote accurate.
  • It does not delete local branches or uncommitted work.
  • It is different from the low-level git prune command.
  • Set fetch.prune=true if you want this cleanup on every fetch.

Course illustration
Course illustration

All Rights Reserved.