Git Housekeeping: Deleting squash merged branches

I’ve been keeping around git branches locally that have been squash merged on Github.

This is a mess when I want to find what branches I may have some uncommitted changes.

This repo has a good solution.

I created a z-shell function:

mygit-delete-squash-merged() {
    git checkout -q master && git for-each-ref refs/heads/ "--format=%(refname:short)" | while read branch; do mergeBase=$(git merge-base master $branch) && [[ $(git cherry master $(git commit-tree $(git rev-parse $branch\^{tree}) -p $mergeBase -m _)) == "-"* ]] && git branch -D $branch; done

PS. it’s useful to remove those old remote branches that were deleted on Github:

git remote prune origin