Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Delete branches matching the provided pattern
#!/bin/bash
if (( $# != 1 ))
then
echo "Not enough arguments supplied."
echo " usage: git-delete-branches <branch pattern to search on>"
exit
fi
BRANCH_PATTERN=$1
if [ -z "$BRANCH_PATTERN" ]
then
echo "A pattern is required."
exit
fi
# Check which arguments have been passed to the script
while getopts u:d:p:f: option
do
case "${option}"
in
v) VERBOSE=true;;
f) FORCE=true;;
esac
done
echo "Searching for branches matching $BRANCH_PATTERN"
MATCHING_BRANCHES=$( git branch --list "$BRANCH_PATTERN" )
if [[ ! -z "$MATCHING_BRANCHES" ]]
then
echo "Found the following, matching branches:"
echo "$MATCHING_BRANCHES"
echo "Deleting matching branches."
echo "$MATCHING_BRANCHES" | xargs git branch -D "${VERBOSE:=--verbose}" "${FORCE:=--force}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.