Skip to content

Instantly share code, notes, and snippets.

@dayne
Created June 2, 2017 17:28
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 dayne/5f8177a77b365675b303f54d5a5a51d3 to your computer and use it in GitHub Desktop.
Save dayne/5f8177a77b365675b303f54d5a5a51d3 to your computer and use it in GitHub Desktop.
remove remote branches script
#!/usr/bin/env bash
# Usage: gina git_cleanup
# Summary: Detect and delete git branches that are merged and can be removed.
# This must be run from master
starting_branch=`git branch | grep ^\* | awk "{ print \$2 }"`
git checkout master
if [[ $? -ne 0 ]]; then
echo "git checkout master failed - bailing out before doing any further activities"
fi
# Update our list of remotes
git fetch
# Remove local fully merged branches
git branch --merged master | grep -v 'master$' | xargs echo "branch(s) to remove: "
read -p "Continue (y/n)? "
if [ "$REPLY" == "y" ]
then
git branch --merged master | grep -v 'master$' | xargs git branch -d
git remote prune origin
echo "Done!"
echo "Obsolete branches are removed"
else
echo "ok, exiting out gracefully"
exit 1
fi
# check to see if starting branch still exists
git branch | grep $starting_branch > /dev/null
if [ $0 -eq 0 ]; then
echo "original starting branch '$starting_branch' still exists - switching back to that"
git checkout $starting_branch
fi
@dayne
Copy link
Author

dayne commented Jun 2, 2017

original authors @grimm and @dayne

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