Skip to content

Instantly share code, notes, and snippets.

@akhileshdarjee
Created August 31, 2022 14:02

Revisions

  1. akhileshdarjee created this gist Aug 31, 2022.
    25 changes: 25 additions & 0 deletions delete_old_git_branches.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    #!/bin/bash

    ##
    # Script to delete remote git branches
    ##

    # Fetch the remote resources
    git fetch

    # Loop through all remote merged branches
    count=0
    date='Sep 1, 2021'
    echo "Branches that are older than $date"

    for branch in $(git branch -r --merged | grep -v HEAD | grep -v develop | grep -v master | grep -v master | sed /\*/d); do
    if [ -z "$(git log -1 --since="$date" -s ${branch})" ]; then
    echo -e `git show --format="%ci %cr %an" ${branch} | head -n 1` \\t$branch
    count=$((count + 1))
    remote_branch=$(echo ${branch} | sed 's#origin/##' )
    # To delete the branches uncomment the bellow git delete command
    #git push origin --delete ${remote_branch}
    fi
    done

    echo "Found $count merged branches that have no commits since $date"