Last active
September 16, 2019 18:40
-
-
Save dccampbell/1f2d2c4de03c108344ce997222e18b0b to your computer and use it in GitHub Desktop.
Git: Prune Merged Branches
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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