Hello,
In this short post I'd like to share with you a simple solution to the issue I'm encountering from time to time. The problem is that each of repositories I'm actively working on is aggregating a lot of branches, because in my workflow, I'm creating a new branch for each new task I'm working with. After merging branch to main
, I want to keep the branch for a while just-in-case, but then, as one would expect, I forget to delete it. So after some time, when I run git branch
I am getting more and more results. Actually, I've decided to do something about it when I saw that I have 61 local branches in one of my local repos.
The solution that came to my mind was to right a shell function and add it to my ~/.zshrc
that would work as command that can remove all of the unused local branches automatically.
I've decided that this command should work in the opposite way to git branch --delete
where you provide a list of branch names you want to delete. So instead, I want to provide the list of branches I want to keep.
After a thought that I'd need to add branch main
or master
each time I'd run this command, I've decided to always add them to the list of branch names that should not be deleted.
Another improvement I've decided to add was to always keep the current branch. Just in case I've decided to clean up all the branches but forgot that I'm already not on the main
branch.
In my case, I've added it to a file called ~/.custom_aliases
that I keep developing for improving productivity and is "imported" inside my ~/.zshrc
.
Here's the code for it:
So if you ran git branch
and see something like π:
and then you run π:
You will end up with clean repo with just the main
branch, current branch, and the branches you explicitly provided not being deleted:
And all that with just a single command. :not_bad:
That's it from me, thanks for reading. I encourage you to leave a like/comment here or star/comment on github. See you! π
Top comments (1)
Thanks for the article! β€οΈ