Skip to content

Instantly share code, notes, and snippets.

@tmcgee123
Last active April 1, 2019 22:33
Show Gist options
  • Save tmcgee123/feef4a7641ee1d9bc15f to your computer and use it in GitHub Desktop.
Save tmcgee123/feef4a7641ee1d9bc15f to your computer and use it in GitHub Desktop.
Clears the branches in current working directory that are not master, develop, or current checkout'd out branch
#!/bin/bash
#go to the current working directory
pwd |
while CWD= read -r line; do
cd "$line"
done
#designate local branches to skip
SKIPPEDBRANCHES="master develop"
#current branch regex, starts with *
CURRENTREGEX="^\*."
#grep local branches list to separate by space
git branch |
while IFS= read -r line; do
CURRENTBRANCH="${line// /}"
SKIPBRANCH="false"
for skippedbranch in $SKIPPEDBRANCHES ; do
#validate current branch name against skipped and not the current branch
if [[ $CURRENTBRANCH == $skippedbranch ]] || [[ $CURRENTBRANCH =~ $CURRENTREGEX ]]; then
SKIPBRANCH="true"
fi
done
#delete branch if passed checks
if [[ "$SKIPBRANCH" == "false" ]]; then
git branch -D "$CURRENTBRANCH"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment