DEV Community

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

Posted on

2 1

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!

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay