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

Daniel Teixeira @dsteixeira

About: Backend Developer interested REST API design and data manipulation.

Location:
Brazil
Joined:
Jul 15, 2024

Remove local branches that are no longer in the origin

Publish Date: Feb 4
0 0

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. :)

Comments 0 total

    Add comment