Skip to content

Instantly share code, notes, and snippets.

@atarola
Last active February 14, 2017 18:19
Show Gist options
  • Save atarola/8ce94d480b9ce23fd8b4 to your computer and use it in GitHub Desktop.
Save atarola/8ce94d480b9ce23fd8b4 to your computer and use it in GitHub Desktop.
Cleanup a Repo
#!/bin/bash
#
# Cleanup both local and remote branches that have been merged to master
#
PREFIX="remotes/origin/"
# make sure you're on master
git checkout master
git pull
# clean the local git
git remote prune origin
for branch in $(git branch -a --merged | grep -v "*"); do
if [[ $branch == *master* || $branch == *HEAD* || $branch == "->" ]]; then
# don't delete master
echo " - skipping: $branch"
continue
fi
if [[ $branch == *origin* ]]; then
echo " - remote branch: $branch"
remote=${branch#$PREFIX}
git push origin :$remote
else
echo " - local branch: $branch"
git branch -d $branch
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment