Created
January 17, 2018 11:34
-
-
Save settermjd/0fdd21b04d18effed727aef82f7e7e50 to your computer and use it in GitHub Desktop.
Delete branches matching the provided pattern
This file contains 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 | |
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