DEV Community

Cover image for Remove local branches that are no longer in the origin
Daniel Teixeira
Daniel Teixeira

Posted on

Remove local branches that are no longer in the origin

This is a simple script that I use to remove local branches that are no longer on the origin. Just to clean my local env.

git remote prune origin
git branch -vv | grep ': gone]' | grep -v "\*" | awk '{ print $1; }' | xargs -r git branch -d
Enter fullscreen mode Exit fullscreen mode

You can also create an alias and add to your .bashrc (if you use Linux or macOS).

Edit ~/.bashrc file.

Add this in the end of the file:

alias git-clean="git remote prune origin; git branch -vv | grep ': gone]' | grep -v \"\*\" | awk '{ print $1; }' | xargs -r git branch -d;"
Enter fullscreen mode Exit fullscreen mode

Reload bash: source ~/.bashrc

That's all. :)

Top comments (0)