Git delete old branches

Published: May 11, 2020

Over time in a project you end up with a lot of old branches. If you want to do some house keeping and only keep branches that exists on the remote server you can follow along.

WARNING

Make sure you don't have any local work you want to keep on a brach that has not been pushed.

Deleting local branches

First you want to delete all local branches, except main.

sh
git branch | grep -v "main" | xargs git branch -D

Deliting remote branches

sh
git remote prune origin

Conclusion

Now you are only left with main and the remote-tracking branches that actually exists in the remote repository. You can see them with:

sh
git branch -a