4

I have a LOT of GIT branches on my "remote" server.

  1. How can I delete ALL branches (Not just merged) that are older than 1 year?
  2. How can I also delete all merged branches (multiple origins "master/develop") older than 5 months?

This answer is quite nice, but it doesn't get me all the way there. How can I delete all Git branches which have been merged?

Can you please include master/develop branches from the merge? How do I add a time interval on this?

git branch -r --merged | grep -v master | sed 's/origin\///' | xargs -n 1 git push --delete origin
SpoiledTechie.com
  • 9,951
  • 19
  • 73
  • 98
  • I know this doesn't quite answer your question, but GitHub has a handy tool for this sort of work if the number of branches isn't too overwhelming. As far as I can tell, though, you have to enter the url manually: https://github.com/cyborgx37/sandbox/branches – JDB still remembers Monica Oct 24 '17 at 16:15
  • 3
    Git doesn't store branch creation date as info. What you could look for is branches to which last commits were 1 year ago. For that you could try using git for-each-ref command: https://git-scm.com/docs/git-for-each-ref – frennky Oct 24 '17 at 17:23
  • I agree with @frennky. Additionally, what do you mean by "older than 1 year"? Most recent commit is older than 1 year old? First commit is older than 1 year old? (The concept of "first commit" is kind of fuzzy in git and may not be in any way obvious depending on what your history looks like.) – Mort Oct 25 '17 at 00:07

1 Answers1

5

You can use shell script to delete no merged branches which are older then one year, and delete the merged branches which are older than five months.

Delete no-merged branches which are older then one year

    #!/bin/bash
    
    tarBranch=$(git branch -r --no-merged | grep -v master | grep -v developer | sed 's/origin\///')
    for branch in $tarBranch
    do
     echo $branch
     lastDate=$(git show -s --format=%ci origin/$branch)
     convertDate=$(echo $lastDate | cut -d' ' -f 1)
     Todate=$(date -d "$convertDate" +'%s')
     current=$(date +'%s')
     day=$(( ( $current - $Todate )/60/60/24 ))
     echo "last commit on $branch branch was $day days ago"
     if [ "$day" -gt 365 ]; then
        git push origin :$branch
        echo "delete the old branch $branch"
     fi
    done

Delete the merged branches which are older than five months

    #!/bin/bash
    
    git checkout master
    #deleted merged branches on master branch
    tarBranch=$(git branch -r --merged | grep -v master | grep -v develop | sed 's/origin\///')
    for branch in $tarBranch
    do
     echo $branch
     lastDate=$(git show -s --format=%ci origin/$branch)
     convertDate=$(echo $lastDate | cut -d' ' -f 1)
     Todate=$(date -d "$convertDate" +'%s')
     current=$(date +'%s')
     day=$(( ( $current - $Todate )/60/60/24 ))
     echo "last commit on $branch branch was $day days ago"
     if [ "$day" -gt 150 ]; then
        git push origin :$branch
        echo "delete the old branch $branch"
     fi
    done

    git checkout develop
    #deleted merged branches on developer branch
    tarBranch=$(git branch -r --merged | grep -v master | grep -v develop | sed 's/origin\///')
    for branch in $tarBranch
    do
     echo $branch
     lastDate=$(git show -s --format=%ci origin/$branch)
     convertDate=$(echo $lastDate | cut -d' ' -f 1)
     Todate=$(date -d "$convertDate" +'%s')
     current=$(date +'%s')
     day=$(( ( $current - $Todate )/60/60/24 ))
     echo "last commit on $branch branch was $day days ago"
     if [ "$day" -gt 150 ]; then
        git push origin :$branch
        echo "delete the old branch $branch"
     fi
    done
blackgreen
  • 4,019
  • 8
  • 23
  • 41
Marina Liu
  • 32,805
  • 4
  • 48
  • 60