Skip to content

Instantly share code, notes, and snippets.

@62mkv
Last active October 26, 2016 06:09
Show Gist options
  • Save 62mkv/a5930d2dd3e7ca81d641 to your computer and use it in GitHub Desktop.
Save 62mkv/a5930d2dd3e7ca81d641 to your computer and use it in GitHub Desktop.
Bash script to remove merged branches both locally and remotely (from "origin" only). Parses all REPOs within current folder, checkouts "master" on each of the REPOs
function remove_remote_branch() {
branch=$1
git push origin --delete $branch
}
function remove_local_branch() {
branch=$1
git branch -d $branch
}
PROTECTED="\(develop\|master\|qa\|sandbox\)"
for folder in `ls -1d */` ; do
cwd=`pwd`
git=${folder%/}
echo "--"
echo "Working with $git:"
cd $git
git checkout master
git pull origin
git remote prune origin
for branch in `git branch -r --merged origin/master | grep -vw $PROTECTED | grep -w "origin" | perl -e "while(<>) { s/origin\///g; s/ //g; print $_;}"` ; do
remove_remote_branch $branch
done
for branch in `git branch --merged origin/master | grep -vw $PROTECTED` ; do
remove_local_branch $branch
done
git remote prune origin
cd ..
done
@62mkv
Copy link
Author

62mkv commented Nov 18, 2015

Processes all the repos from the current folder!

@62mkv
Copy link
Author

62mkv commented Dec 21, 2015

Added local branches purging

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment