Another month, another git alias (But don’t think that this will become a regularity now ;-) ). I had the issue that I lost track of the branches that were already in master a couple of times on one of my projects and after a while I had a lot of unnecessary jobs in Jenkins because we use the Jenkins multibranch plugin. So I sat down and created a git alias that does the cleanup for you with a bit of user-friendly output.
The strategy is pretty simple. We can figure out which branches are already merged into master by executing:
As we want to filter out the branch we are currently on (because we aren’t able to delete it anyway) and the master branch itself we pipe it through
For the resulting list we simply need to execute
git branch -d to delete the branches locally and
git push --delete origin to delete them on remote as well.
With some additional user feedback and safety nets we and up with a script like this:
At last we also want to use it as a git alias so we add it with: