Skip to content

Instantly share code, notes, and snippets.

@conquests
Forked from ryanc414/git-rm-merged
Created July 12, 2022 12:17
Show Gist options
  • Save conquests/6269a591452b3b45de895181a9ae1e4b to your computer and use it in GitHub Desktop.
Save conquests/6269a591452b3b45de895181a9ae1e4b to your computer and use it in GitHub Desktop.
Delete merged git branches locally and on remote
#!/bin/bash
# Remove merged git branches locally and on remote(s)
set -e
# Script constants
BRANCH_WHITELIST="(\*|master|other-special-branches)"
REMOTES="origin" # Add more remotes here as space-separated list
echo "Deleting merged branches locally"
git branch --merged master \
| egrep -v "$BRANCH_WHITELIST" \
| xargs -r git branch -d
# Delete merged branches from each remote.
for remote in $REMOTES; do
echo "Deleting merged branches from $remote"
git fetch "$remote" --prune
for branch in $(git branch -r --merged master \
| grep "$remote" \
| egrep -v "$BRANCH_WHITELIST"); do
git push "$remote" --delete "${branch#*/}"
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment