Skip to content

Instantly share code, notes, and snippets.

@edorian
Created June 14, 2012 07:39
Show Gist options
  • Save edorian/2928778 to your computer and use it in GitHub Desktop.
Save edorian/2928778 to your computer and use it in GitHub Desktop.
Delete merged and stale git branches
#!/bin/bash
if [ ! -d "$1" ]; then
echo "Usage: "$0" path/to/git/checkout"
exit 1
fi
cd $1
LOG="tee -a /tmp/branch-cleanup.log"
git checkout master
git fetch
git remote prune origin
branchlist=`git branch -r --merged master | sed 's/ *origin\///' | grep -v 'master$'`
for branch in $branchlist
do
commitHappendInTheLast4Weeks=`git log -n1 --since="4 weeks ago" "origin/"$branch`
if [ "$commitHappendInTheLast4Weeks" == "" ]
then
echo "To be deleted: "$branch | $LOG
echo `git log -n1 "origin/"$branch` | $LOG
# Comment out the following if you don't trust random scripts on the net and just want to see what could be deleted :)
git push origin ":"$branch | $LOG
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment