Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kmuncie/3e20b911732177164da1cdd81dd808bf to your computer and use it in GitHub Desktop.
Save kmuncie/3e20b911732177164da1cdd81dd808bf to your computer and use it in GitHub Desktop.
Script to delete branches older than a certain date
#!/bin/sh
date=$1
DRY_RUN=1
for branch in $(git branch -a | sed 's/^\s*//' | sed 's/^remotes\///' | grep -v 'master$'); do
if [[ "$(git log $branch --since $date | wc -l)" -eq 0 ]]; then
if [[ "$branch" =~ "origin/" ]]; then
local_branch_name=$(echo "$branch" | sed 's/^origin\///')
if [[ "$DRY_RUN" -eq 1 ]]; then
echo "git push origin :$local_branch_name"
else
git push origin :$local_branch_name
fi
else
if [[ "$DRY_RUN" -eq 1 ]]; then
echo "git branch -D $branch"
else
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