How to remove unused (stale) branches in Git

How to remove unused (stale) branches in Git

Many times while completing a pull request we delete the source branch, this deletes the branch from the git server, but still, the reference to that branch would appear in Visual Studio. Even if you do a fetch, pull or sync, the deleted branch from the server would still appear in Visual Studio.


As you can see in the above image, a deleted branch (feature1 branch) still shows up in visual studio.

So to remove, first let's check which branches have to be pruned (remove reference). Run

git remote prune origin --dry-run


The argument --dry-run just mimics the prune and does not actually remove it.

Now to remove the branches, run the prune command without the --dry-run argument.

git remote prune origin


If you go ahead and check in visual studio, you wouldn't find the pruned branches inside remote.


If you want to remove the local branch as well; right-click on that branch in visual studio and choose delete from the context menu.

Alternatively, you could also run

git branch -d feature1

in a terminal window.


That's it!! You have removed references to your unused local and remote branches!