Skip to content

Instantly share code, notes, and snippets.

Created January 21, 2013 14:29
  • Star 16 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save antonio/4586456 to your computer and use it in GitHub Desktop.
Script to delete branches older than a certain date
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"
git push origin :$local_branch_name
if [[ "$DRY_RUN" -eq 1 ]]; then
echo "git branch -D $branch"
git branch -D $branch
Copy link

how is the date format?

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

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:

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