Skip to content

Instantly share code, notes, and snippets.

@jaredhabeck
Last active May 11, 2016 15:34
Show Gist options
  • Save jaredhabeck/3801d7b540c7bf43c5508a0fa47db990 to your computer and use it in GitHub Desktop.
Save jaredhabeck/3801d7b540c7bf43c5508a0fa47db990 to your computer and use it in GitHub Desktop.
This script is for cleaning up merged branches on remote, it will only check merged and also outputs relevant information, then prompts for action. It also prunes the origin if deletes were made.
#!/bin/bash
count=0
# grab remote merged branches, ignoring master and HEAD
for x in `git branch --merged master -r --column | grep -v HEAD | grep -v master`; do
branch="${x/origin\//}"
mod=$(git show --pretty=format:"%Cgreen%ci %Cblue%cr%Creset" $x | head -n 1)
# get the number of commits the remote branch is behind/ahead of master
set -- $(git rev-list --left-right --count origin/$branch...origin/master)
ahead=$1
behind=$2
printf "\033[0;35m$branch\033[0m\n"
echo "Last Commit: "$mod
echo "Commits behind: " $behind " Ahead: " $ahead
read -p "Delete? [Y/n/q]" -n1 option
case "$option" in
""|y|Y)
count=$((count+1))
git push origin :$branch
;;
n|N)
echo
continue
;;
q|Q)
break
;;
esac
done
if [ "$count" -gt 0 ]; then
echo
echo "Pruning origin."
git remote prune origin
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment