简体   繁体   English

如何删除所有超过一年的 Git 远程分支?

[英]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.我的“远程”服务器上有很多 GIT 分支。

  1. How can I delete ALL branches (Not just merged) that are older than 1 year?如何删除超过 1 年的所有分支(不仅仅是合并)?
  2. How can I also delete all merged branches (multiple origins "master/develop") older than 5 months?我怎样才能删除所有超过 5 个月的合并分支(多个来源“master/develop”)?

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? 如何删除所有已合并的 Git 分支?

Can you please include master/develop branches from the merge?您能否包括合并中的 master/develop 分支? 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

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.您可以使用 shell 脚本删除超过一年的合并分支,并删除超过五个月的合并分支。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM