Skip to content

Instantly share code, notes, and snippets.

@dccampbell
Last active September 16, 2019 18:40
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 dccampbell/1f2d2c4de03c108344ce997222e18b0b to your computer and use it in GitHub Desktop.
Save dccampbell/1f2d2c4de03c108344ce997222e18b0b to your computer and use it in GitHub Desktop.
Git: Prune Merged Branches
#!/bin/bash
set -e
git fetch --all --prune
localBranches=$(git branch --merged origin/master | sed 's/^[ *]\+//' | grep -v '^master$' || echo '')
if [[ -z "$localBranches" ]]; then
echo "No local merged branches found."
else
echo "Local Merged Branches:"
echo -e "\n${localBranches}\n"
read -p "Delete these local branches (y/N)?"
if [[ "$REPLY" == "y" || "$REPLY" == "Y" ]]; then
echo "$localBranches" | xargs -r -d'\n' git branch --delete
fi
fi
remoteBranches=$(git branch --remotes --merged origin/master | sed 's/^[ *]\+//' | grep '^origin/' | grep -v '/master$' | sed 's/^origin\///' || echo '')
if [[ -z "$remoteBranches" ]]; then
echo "No remote merged branches found."
else
echo "Remote Merged Branches:"
echo -e "\n${remoteBranches}\n"
read -p "Delete these remote branches (y/N)?"
if [[ "$REPLY" == "y" || "$REPLY" == "Y" ]]; then
echo "$remoteBranches" | xargs -r -d'\n' git push --delete origin
fi
fi
git fetch --all --prune
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment