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:
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:
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 --prunecleans up stale remote-tracking refs' - '
git pruneis 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:
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:
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:
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:
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 --pruneremoves 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 prunecommand. - Set
fetch.prune=trueif you want this cleanup on every fetch.

