How to prune remote branches in Git
Pruning remote branches removes references to branches that have been deleted on the remote repository but still appear in your local repository. As the creator of CoreUI with 25 years of Git experience managing hundreds of contributors, I prune remote branches regularly to keep repositories clean.
The most effective command is git fetch --prune or git remote prune origin.
Direct Answer
Use git fetch --prune to update and clean remote references in one command.
git fetch --prune
Alternative Command
Prune a specific remote without fetching:
git remote prune origin
Automatic Pruning
Configure Git to automatically prune on every fetch:
git config --global fetch.prune true
View Stale Branches
See which branches will be pruned before removing them:
git remote prune origin --dry-run
Complete Cleanup
Remove local branches that track deleted remote branches:
# List merged branches
git branch -vv | grep ': gone]' | awk '{print $1}'
# Delete them
git branch -vv | grep ': gone]' | awk '{print $1}' | xargs git branch -d
Explanation
When you delete a branch on GitHub or another remote, your local Git still keeps a reference to origin/branch-name. Running git fetch --prune removes these stale references, cleaning up your branch list. The --dry-run flag lets you preview what will be removed before actually deleting anything.
Best Practice Note
This is the same branch cleanup strategy we use in CoreUI’s repositories with hundreds of contributors. Regular pruning keeps your git branch -r output clean and prevents confusion about which branches actually exist on the remote. Set fetch.prune true globally to automate this maintenance.
Related Articles
For related Git maintenance, check out how to set upstream branch in Git and how to change remote URL in Git.



