Last active
January 15, 2020 15:32
-
-
Save kyle-west/6ffc7ec0d1c377c99f44c5388356391f to your computer and use it in GitHub Desktop.
Remove all the Git branches that match a pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Installation
This script is best installed with the
bashful
CLI.Usage
For best results, use
prune
from themaster
branchIf we wanted to remove all the branches that have the word
fix
in the name type:The following menu will appear
Answer
y
to delete those branches and any other key to cancel.To remove all branches except master, just run
prune
with no argumentsYou cannot remove the
master
branch with this tool nor the current branch you are on