How to remove unused (stale) branches in Git

Subscribe to my newsletter and never miss my upcoming articles

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.

server-branches1-1.pngvisual-studio-branch-status-1.png

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

prune-dry-run1-1.png

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

git-prune-1.png

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

post-pruning-visual-studio-1.png

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.

delete-local-branch-1.png

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

No Comments Yet