Skip to content

Instantly share code, notes, and snippets.

@mri-dula
Created October 22, 2020 09:28
Show Gist options
  • Save mri-dula/f37e54828d2cfeae1c22e1da52cc9ba4 to your computer and use it in GitHub Desktop.
Save mri-dula/f37e54828d2cfeae1c22e1da52cc9ba4 to your computer and use it in GitHub Desktop.
GIT: Delete all (except specific) branches from your local repo
#!/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
@mri-dula
Copy link
Author

Usage:

$ delete_multiple_branches

This will delete all the branches except master and your current branch.

$ delete_multiple_branches branch1 branch2

This will delete all the branches except master, branch1, branch2 and your current branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment