Clean Remote Branches With Prune | Generated by AI
git fetch --prune is a way to keep your local references to remote branches clean and up-to-date.
What it does
git fetchnormally updates your local copy of the remote’s branches (likeorigin/main,origin/feature-x) without touching your own local branches.- Over time, the remote repository might delete or rename some branches. Without cleanup, your local repo will still show those old remote-tracking branches (e.g.,
origin/feature-old). - Adding
--prunetells Git: “If the remote no longer has a branch, delete my corresponding remote-tracking reference.”
So, after running git fetch --prune, your local list of remote branches matches exactly what the remote currently has.
Example
- Remote has branches:
main,feature-a. - You run
git fetch→ you now haveorigin/main,origin/feature-a. - Someone deletes
feature-aon the remote. - If you run
git fetch, you’ll still seeorigin/feature-alocally (stale reference). - If you run
git fetch --prune, Git will noticefeature-ais gone and removeorigin/feature-afrom your local repo.
Why it’s useful
- Keeps
git branch -r(remote branches list) clean. - Avoids confusion when switching or merging into branches that no longer exist upstream.
-
Often used with aliases (e.g.,
git fetch -p) or set as default with:git config fetch.prune true
That way, every git fetch automatically prunes stale branches.
Do you want me to also explain how pruning interacts with tags (since git fetch --prune only handles branches by default, but tags behave differently)?