Skip to content

Instantly share code, notes, and snippets.

@antonio
Created January 21, 2013 14:29
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save antonio/4586456 to your computer and use it in GitHub Desktop.
Save antonio/4586456 to your computer and use it in GitHub Desktop.
Script to delete branches older than a certain date
#!/bin/sh
date=$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
@redthor
Copy link

redthor commented Apr 13, 2018

I've created an alternative that will make (I think) one git request to the remote instead of one for each branch:

https://gist.github.com/redthor/3776c1f726cafff41c9eda6f271466c3

Also the date format that works is like "2018-01-01" (using "2 weeks ago" with spaces will fail)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment