Skip to content

Instantly share code, notes, and snippets.

@settermjd
Created January 17, 2018 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save settermjd/0fdd21b04d18effed727aef82f7e7e50 to your computer and use it in GitHub Desktop.
Save settermjd/0fdd21b04d18effed727aef82f7e7e50 to your computer and use it in GitHub Desktop.
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