Skip to content

Instantly share code, notes, and snippets.

@EvanLovely
Last active August 29, 2015 14:06
Show Gist options
  • Save EvanLovely/22e5965777652372b04b to your computer and use it in GitHub Desktop.
Save EvanLovely/22e5965777652372b04b to your computer and use it in GitHub Desktop.

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
# 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!"
}
{
"tags": [
'bash',
'bash__functions',
'git',
'utilities'
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment