Skip to content

Instantly share code, notes, and snippets.

@ryanshoover
Created March 30, 2016 16:55
Show Gist options
  • Save ryanshoover/ce2645a9ebef9b3d2f56eb40318e8a10 to your computer and use it in GitHub Desktop.
Save ryanshoover/ce2645a9ebef9b3d2f56eb40318e8a10 to your computer and use it in GitHub Desktop.
Clean out dead branches from local and remote repos
#!/bin/bash
## Typical usage
# gitclean
# gitclean remote
say() { echo "$0: $*" >&2; }
die() { say "$*"; exit 111; }
try() { "$@" || die "cannot $*"; }
# Clean GIT repo of all merged branches
git checkout staging;
git pull origin staging;
if [ "$1" == "remote" ]
then
echo "Cleaning " $1
git branch --remote --list 'dev1/MAR\-*' --merged | xargs git branch -rd
git branch --remote --list 'dev2/MAR\-*' --merged | xargs git branch -rd
git branch --remote --list 'dev3/MAR\-*' --merged | xargs git branch -rd
elif [ "$1" == "github" ]
then
echo "Cleaning " $1
git branch --remote --list 'origin/MAR-*' --merged | sed "s/origin\/MAR/:MAR/" | xargs git push origin
else
echo "Cleaning local"
git branch --list 'MAR\-*' --merged | xargs -J % git branch -d %
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment