-
-
Save redbonzai/7d0b1fe615e3bee6d5377d934eed8642 to your computer and use it in GitHub Desktop.
A script for cleaning up Git repositories; it deletes branches that are fully merged into `origin/master`, prunes obsolete remote tracking branches, and as an added bonus will replicate these changes on the remote.
This file contains 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
#!/bin/bash | |
# git-cleanup-repo | |
# | |
# Author: Rob Miller <[email protected]> | |
# Adapted from the original by Yorick Sijsling | |
git checkout master &> /dev/null | |
# Make sure we're working with the most up-to-date version of master. | |
git fetch | |
# Prune obsolete remote tracking branches. These are branches that we | |
# once tracked, but have since been deleted on the remote. | |
git remote prune origin | |
# List all the branches that have been merged fully into master, and | |
# then delete them. We use the remote master here, just in case our | |
# local master is out of date. | |
git branch --merged origin/master | grep -v 'master$' | xargs git branch -d | |
# Now the same, but including remote branches. | |
MERGED_ON_REMOTE=`git branch -r --merged origin/master | sed 's/ *origin\///' | grep -v 'master$'` | |
if [ "$MERGED_ON_REMOTE" ]; then | |
echo "The following remote branches are fully merged and will be removed:" | |
echo $MERGED_ON_REMOTE | |
read -p "Continue (y/N)? " | |
if [ "$REPLY" == "y" ]; then | |
git branch -r --merged origin/master | sed 's/ *origin\///' \ | |
| grep -v 'master$' | xargs -I% git push origin :% 2>&1 \ | |
| grep --colour=never 'deleted' | |
echo "Done!" | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment