Skip to content

Instantly share code, notes, and snippets.

@kyle-west
Last active January 15, 2020 15:32
Show Gist options
  • Save kyle-west/6ffc7ec0d1c377c99f44c5388356391f to your computer and use it in GitHub Desktop.
Save kyle-west/6ffc7ec0d1c377c99f44c5388356391f to your computer and use it in GitHub Desktop.
Remove all the Git branches that match a pattern
#!/bin/bash
branches=""
for branch in $@; do
branches="$branches $(git branch | grep -v "master" | grep -v '*' | grep "$branch")";
done;
echo "Prune would permenantly remove the following branches from your machine:";
paste -s -d" " <(echo "$branches");
printf 'Are all these ok to remove? [y/N] : '
read -r yN
if [ "$yN" == "y" ] || [ "$yN" == "Y" ]; then
echo "Removing branches...";
for branch in $branches; do
git branch -D "$branch"
done;
else
echo "Action aborted because answer was not positive."
fi
@kyle-west
Copy link
Author

kyle-west commented Mar 13, 2019

Installation

This script is best installed with the bashful CLI.

bashful gist install https://gist.github.com/kyle-west/6ffc7ec0d1c377c99f44c5388356391f

Usage

prune <branch-name-matching-string> [...<branch-name-matching-string>]

For best results, use prune from the master branch

If we wanted to remove all the branches that have the word fix in the name type:

prune fix

The following menu will appear

Prune would permenantly remove the following branches from your machine:
  fix-issue-123   fix-issue-456
Are all these ok to remove? [y/N] :

Answer y to delete those branches and any other key to cancel.

To remove all branches except master, just run prune with no arguments

You cannot remove the master branch with this tool nor the current branch you are on

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment