EvanLovely
9/11/2014 - 8:50 PM

git-branch-cleanup.sh

Useage

Run in a git repo to delete all local branches that have already been merged into a branch you specify.

git-branch-cleanup

Installation

Just add the code to your ~/.bash_functions file and open a new Terminal. Easily done by copying the code and running this:

pbpaste >> ~/.bash_functions
{
  "tags": [
    'bash',
    'bash__functions',
    'git',
    'utilities'
  ]
}
# Delete all git branches merged into master
git-branch-cleanup() {
  echo "Do you want to delete remote branches? [y/n]"
  read remote
  echo "Do you want to delete local branches? [y/n]"
  read local
  echo "Which branch do you want to have as the base? All branches merged into this branch will be deleted. Defaults to master."
  read branchBase
  git checkout $branchBase
  IFS=$'\n'
  for i in $(git branch --merged | grep -v "^*"); do
    branch="$(echo "$i" | tr -d ' ')"
    echo "Deleting $branch"
    if [[ "$remote" == "y" ]]; then
      git push origin --delete $branch
    fi
    if [[ "$local" == "y" ]]; then
      git branch -d $branch
    fi
  done
  unset IFS
  echo "All Done!"
}