I’m using zsh and therefore I have a .zshrc
one of the things I have in my .zshrc
is a small function to cleanup old git branches:
git-cleanup() {
? git fetch -p && for branch in `git branch -vv | grep ': gone]' | awk '{print $1}'`; do git branch -D $branch; done
}
If you copy the above code snippet to your .zshrc
, .bashrc
or .profile
you can call git-cleanup and the code runs.
This function deletes all local branches which are merged remotely. The problem is, my normal workflow is to start a new ticket, change the task in PHPstorm, automatically create a new branch and then start analysing the problem. Often enough I end up with a problem which lives in the database, can be changed via config or is a misunderstanding of the user (often enough myself).
The consequence is, that I have a lot of branches without any commit. I googled a few times how to clean them up, maybe I even found a solution for that, but I was never smart enough to add it to my little cleanup function, but today was the day!
Thanks to Nicky Meuleman and his blogpost “Clean up old git branches”
We want to delete multiple git branches. And I added the snippet to my little function. If you only want to add the snippet by Nicky, make sure to think about adding a git pull or git fetch in the beginning!
And my new snippet looks like this:
git-cleanup() {
? git fetch -p && for branch in `git branch -vv | grep ': gone]' | awk '{print $1}'`; do git branch -D $branch; done
? git branch --merged | egrep -v "(^\*|master|dev)" | xargs git branch -d
}
One thought on “git: Cleanup old/empty branches”