Created
June 2, 2017 17:28
-
-
Save dayne/5f8177a77b365675b303f54d5a5a51d3 to your computer and use it in GitHub Desktop.
remove remote branches script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
original authors @grimm and @dayne