Skip to content

Instantly share code, notes, and snippets.

@nanoxd
Last active April 28, 2017 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nanoxd/015ade1abc089f27bbb62b91f0e0f80e to your computer and use it in GitHub Desktop.
Save nanoxd/015ade1abc089f27bbb62b91f0e0f80e to your computer and use it in GitHub Desktop.
Clean up branches
#!/bin/bash
# git-sweep
# Cleans up branches on Origin and verifies that they have already been merged
# USAGE: git sweep
# This must be run from master
git checkout master
# Update our list of remotes
git fetch
git remote prune origin
# Remove local fully merged branches
git branch --merged master | grep -v 'master$' | xargs git branch -d
# Show remote fully merged branches
remotes_to_prune_count=`git branch -r --merged master | sed 's/ *origin\///' | grep -v 'master$' | wc -l`
if [ $remotes_to_prune_count -gt 0 ]
then
echo "The following remote branches are fully merged and will be removed:"
git branch -r --merged master | sed 's/ *origin\///' | grep -v 'master$'
read -p "Continue (y/n)? "
if [ "$REPLY" == "y" ]
then
# Remove remote fully merged branches
git branch -r --merged master | sed 's/ *origin\///' \
| grep -v 'master$' | xargs -I% git push origin :%
echo "Done!"
fi
else
echo "No remotes to prune. Done!"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment