Skip to content

Instantly share code, notes, and snippets.

@appinteractive
Last active August 29, 2015 14:22
Show Gist options
  • Select an option

  • Save appinteractive/68a736aeabaa92290705 to your computer and use it in GitHub Desktop.

Select an option

Save appinteractive/68a736aeabaa92290705 to your computer and use it in GitHub Desktop.
delete merged branches before specified date.
#!/usr/bin/env bash
# usage:
# sh ./gitclean.sh <date> <run>
# date: format like 2015-06-03, all merged branches before this date will be deleted
# run: when you do NOT add "run" as a secound parameter,
# then the script is run in DRY mode (just output, nothing deleted)
# when you've been asked multiple times for the reposetory password, check out the folowing link
# http://stackoverflow.com/questions/5343068/is-there-a-way-to-skip-password-typing-when-using-https-github
date=$1
if [[ "$date" -eq NULL ]]; then
echo "you need to specify a date in the format yyyy-mm-dd!";
exit;
fi
run=$2;
for branch in $(git branch -a | sed 's/^\s*//' | sed 's/^remotes\///' | grep -v 'master$' | grep -v 'release$'); do
if [[ "$(git log $branch --since $date | wc -l)" -eq 0 ]]; then
# skip release branches
if [[ "$branch" =~ "release/" ]]; then
continue;
fi
if [[ "$branch" =~ "origin/" ]]; then
# strip remotes and origin from branch name
local_branch_name=$(echo "$branch" | sed 's/^remotes\///' | sed 's/^origin\///')
echo "git push origin --delete $local_branch_name"
if [[ "$run" = "run" ]]; then
git push origin --delete $local_branch_name
fi
else
echo "git branch -D $branch"
if [[ "$run" = "run" ]]; then
git branch -D $branch
fi
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment