Skip to content

Instantly share code, notes, and snippets.

@kristw
Created September 2, 2020 18:01
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 kristw/5fa62974fa9956ea73ec50447e91aab3 to your computer and use it in GitHub Desktop.
Save kristw/5fa62974fa9956ea73ec50447e91aab3 to your computer and use it in GitHub Desktop.
#!/bin/bash
function git_clean_local_branches {
OPTION="-d";
if [[ "$1" == "-f" ]]; then
echo "WARNING! Removing with force";
OPTION="-D";
fi;
TO_REMOVE=`git branch -r | awk "{print \\$1}" | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk "{print \\$1}"`;
if [[ -n "$TO_REMOVE" ]]; then
echo "Removing branches...";
echo "";
printf "\n$TO_REMOVE\n\n";
echo "Proceed?";
select result in Yes No; do
if [[ "$result" == "Yes" ]]; then
echo "Removing in progress...";
echo "$TO_REMOVE" | xargs git branch "$OPTION";
if [[ "$?" -ne "0" ]]; then
echo ""
echo "Some branches was not removed, you have to do it manually!";
else
echo "All branches removed!";
fi
fi
break;
done;
else
echo "You have nothing to clean";
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment