Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I delete all Git remote branches which are older than a year?

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
like image 445
SpoiledTechie.com Avatar asked Oct 24 '17 15:10

SpoiledTechie.com


People also ask

How do I clean up remote branches?

In order to clean up remote tracking branches, meaning deleting references to non-existing remote branches, use the “git remote prune” command and specify the remote name. In order to find the name of your current configured remotes, run the “git remote” command with the “-v” option.

What is the git command to delete all the remote branches?

Remote Git branch delete command Alternatively, you can issue either of the following Git commands to delete a remote branch: git push origin --delete <branch> git push origin -d <branch>

How delete multiple remote branches?

Deleting Multiple Remote Branches egrep -v "(^\*|master|main)" - exclude branch master and main. sed 's/origin\///' - because the remote branches are prefixed with origin/ this command will filter that part out so it returns only the branch name. xargs -n 1 git push origin --delete - deletes the remaining branches.


1 Answers

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
like image 120
Marina Liu Avatar answered Nov 15 '22 05:11

Marina Liu