Skip to content

Instantly share code, notes, and snippets.

@LefterisJP
Created March 20, 2019 15:03
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 LefterisJP/193681b8d1d6731bcafe930a833e3fc3 to your computer and use it in GitHub Desktop.
Save LefterisJP/193681b8d1d6731bcafe930a833e3fc3 to your computer and use it in GitHub Desktop.
A script to cleanup old branches and other stuff in git
#!/bin/sh
# Default is 6 months
MONTHS=6
# Default is not dry run
DRY_RUN=
# Default is to ask for confirmation
ASK_CONFIRMATION=1
# Default is to prune remotes
PRUNE_REMOTES=1
usage()
{
echo "usage: $0 [[[-m months ] [-n]] [-h] [-y]]"
}
while [ "$1" != "" ]; do
case $1 in
-m | --months ) shift
MONTHS=$1
;;
-n | --dry-run ) DRY_RUN=1
;;
-y | --no-confirm ) ASK_CONFIRMATION=
;;
-p | --no-prune ) PRUNE_REMOTES=
;;
-h | --help ) usage
exit
;;
* ) usage
exit 1
esac
shift
done
if [[ "$PRUNE_REMOTES" = "1" ]]; then
for remote in $(git remote); do
echo "Pruning remote $remote references"
git remote prune $remote
done
fi
echo "Will delete branches older than $MONTHS months"
for branch in $(git branch -a | sed 's/^\s*//' | grep -v 'master$'); do
if [[ "$(git log $branch --since "$MONTHS months ago" | wc -l)" -eq 0 ]]; then
# Make sure to get only local branches
local_branch_name=$(echo "$branch" | sed 's/remotes\/.*\///')
if [[ "$DRY_RUN" = "1" ]]; then
echo "DRY: Will delete $local_branch_name"
else
if [[ "$ASK_CONFIRMATION" = "1" ]]; then
read -p "Will delete $local_branch_name. Are you sure? " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
git branch -D "$local_branch_name"
else
echo "Did not delete $local_branch_name"
fi
else
git branch -D "$local_branch_name"
fi
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment