Skip to content

Instantly share code, notes, and snippets.

@demonar
Created August 22, 2023 15:56
Show Gist options
  • Save demonar/a0a3867b85d2907b3e37f84152bdba5b to your computer and use it in GitHub Desktop.
Save demonar/a0a3867b85d2907b3e37f84152bdba5b to your computer and use it in GitHub Desktop.
This script deletes branches that doesn't exist on the remote
cd $1
declare -a BRANCHES=(`git for-each-ref --format='%(refname:short)' refs/heads/`)
CURRENT=(`git rev-parse --abbrev-ref HEAD`)
for BRANCH in "${BRANCHES[@]}"
do
EXISTS=(`git ls-remote --heads origin refs/heads/"$BRANCH" | wc -l`)
if [ "$CURRENT" != "$BRANCH" ] && [ "$EXISTS" = 0 ]
then
git branch -D "$BRANCH"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment