DEV Community

Cover image for How to clean up git branches in a sweep
Patrice Gauthier
Patrice Gauthier

Posted on

How to clean up git branches in a sweep

You have been working a while and have created a lot branches. They are piling up. You forgot about them but they impede your productivity.
You want to push your branch but the autocomplete in terminal is stopping at the last character because it can't guess which branch you mean.

You have too much branches locally.

I have a simple trick to that allows you to delete many branches at once while selecting which one using the NPM project ipt

First install it by doing

npm i -g ipt
Enter fullscreen mode Exit fullscreen mode

Now in the repo you want to delete branches do:

git branches|ipt -m|xargs -I{} git branch -d {}
Enter fullscreen mode Exit fullscreen mode

or -D {} to force delete.

console

The above first list all the branches piping the list to ipt (with the select multiple option) then a list of the selected will be piped to xargs that will do a git branch -d <branch> which will delete each branch 🎉

Bonus: Using the git extension of ohmyzsh

gb|ipt -m|xargs -I{} git branch -d {}
Enter fullscreen mode Exit fullscreen mode

sadly using aliases (gb) doesn't work with xargs.

Thats'all!

Top comments (0)