#!/usr/bin/env zsh function removeCurrentBranch { sed -E '/\*/d' } function removeMaster { sed -E '/master/d' } function leftTrim { sed -E 's/\*?[[:space:]]+//' } # Clean up all cleanly merged branches for branch in $(git branch | removeCurrentBranch | removeMaster | leftTrim); do git rebase origin/master $branch &> /dev/null || git rebase --abort &> /dev/null if (git cherry origin/master $branch | grep '^+') { echo $branch is not merged } else { echo Deleting branch: $branch } done git branch --merged | removeCurrentBranch | removeMaster | leftTrim | xargs -n 1 git branch -d # Ask about which other branches to delete interactively file="/tmp/git-cleanup-branches-$(uuidgen)" all_branches=($(git branch | removeCurrentBranch | leftTrim)) # write branches to file for branch in $all_branches; do echo "keep $branch" >> $file done # write instructions to file echo " # All of your branches are listed above # (except for the current branch, which you can't delete) # change keep to d to delete the branch # all other lines are ignored" >> $file # prompt user to edit file vim "$file" # check each line of the file cat $file | while read -r line; do # if the line starts with "d " if echo $line | grep --extended-regexp "^d " > /dev/null; then # delete the branch branch=$(echo $line | sed -E 's/^d //') git branch -D $branch fi done # clean up rm $file