Created
June 3, 2013 23:54
-
-
Save hatchetation/5702491 to your computer and use it in GitHub Desktop.
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 | |
set -e | |
# cleanup a git repo, removing fully merged branches (locally and remotely) | |
# based on http://devblog.springest.com/a-script-to-remove-old-git-branches | |
# run from the repo to be cleaned | |
# TODO: store lists of branches, DRY (don't want list to change after confirm) | |
COMPARE_BRANCH='master' # branch to compare other branches against | |
# This has to be run from branch being compared against | |
git checkout ${COMPARE_BRANCH} | |
# Update our list of remotes, fetching new, and pruning stale | |
git fetch | |
git remote prune origin | |
# Remove local fully merged branches | |
# TODO: script will abort here if there are no local branches to remove | |
echo "The following local branches are fully merged and will be removed:" | |
git branch --merged ${COMPARE_BRANCH} | \ | |
grep -v "${COMPARE_BRANCH}\$" | \ | |
grep -v "master\$" | |
read -p "Continue (y/n)? " | |
if [ "$REPLY" == "y" ] | |
then | |
git branch --merged ${COMPARE_BRANCH} | \ | |
grep -v "${COMPARE_BRANCH}\$" | \ | |
grep -v "master\$" | \ | |
xargs -I% git branch -d % | |
fi | |
# Show remote fully merged branches | |
echo "The following remote branches are fully merged and will be removed:" | |
git branch -r --merged ${COMPARE_BRANCH} | \ | |
sed 's/ *origin\///' | \ | |
grep -v "${COMPARE_BRANCH}\$" | \ | |
grep -v "master\$" | |
read -p "Continue (y/n)? " | |
if [ "$REPLY" == "y" ] | |
then | |
# Remove remote fully merged branches | |
# TODO: `grep -v "master\$" : skips long-running braches that should remain. add lists | |
git branch -r --merged ${COMPARE_BRANCH} | \ | |
sed 's/ *origin\///' | \ | |
grep -v "${COMPARE_BRANCH}\$" | \ | |
grep -v "master\$" | \ | |
xargs -I% git push origin :% | |
echo "Done!" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment