Created
October 22, 2020 09:28
-
-
Save mri-dula/f37e54828d2cfeae1c22e1da52cc9ba4 to your computer and use it in GitHub Desktop.
GIT: Delete all (except specific) branches from your local repo
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 | |
function exit_if_nothing { | |
if [ -z "$list" ]; then | |
echo "Nothing to delete" | |
exit 0 | |
fi | |
} | |
list=`git branch --list | cat | grep -v '\*' | grep -v "\master\b"` | |
exit_if_nothing | |
for i in $*; do | |
list=`grep -v "\b$i\b" <<< $list` | |
done | |
list=`echo $list | paste -s -d " " -` | |
exit_if_nothing | |
git branch -D $list |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
This will delete all the branches except
master
and your current branch.This will delete all the branches except
master
,branch1
,branch2
and your current branch.