Skip to content

Instantly share code, notes, and snippets.

@nilandev
Created July 16, 2020 19:57
Show Gist options
  • Save nilandev/2cb74db9b7bb73625507aa7929c8b49a to your computer and use it in GitHub Desktop.
Save nilandev/2cb74db9b7bb73625507aa7929c8b49a to your computer and use it in GitHub Desktop.
Delete all other local branches except master, develop or release/*
#!/bin/bash
# Move to master branch. Delete all other local branches except master, develop, release/* or project/*
# Move to master branch
git checkout master
# Collect branches
branches=()
eval "$(git for-each-ref --shell --format='branches+=(%(refname))' refs/heads/)"
for branch in "${branches[@]}"; do
old="refs/heads/"
branchName=${branch/$old/}
if [[ "$branchName" != "master" && "$branchName" != "develop" && "$branchName" != "release/"* ]]; then
git branch -D $branchName
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment