Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@CavalcanteLeo
Copy link

CavalcanteLeo commented May 9, 2017

how is the date format?

@brand-it
Copy link

brand-it commented Apr 2, 2018

It uses the git since syntax. Here is what I found in the examples section of the man log

git log --since="2 weeks ago" -- gitk
   Show the changes during the last two weeks to the file gitk. The "--" is necessary to avoid confusion with the branch named gitk

@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