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

时间:2017-10-24 15:45:54

标签: git github version-control branch feature-branch

我的“远程”服务器上有很多GIT分支。

  1. 如何删除超过1年的所有分支(不仅仅是合并)?
  2. 如何删除超过5个月的所有合并分支(多个来源“master / develop”)?
  3. 这个答案非常好,但它并没有让我一路走来。 How can I delete all Git branches which have been merged?

    您可以在合并中包含master / develop分支吗?如何在此添加时间间隔?

    git branch -r --merged | grep -v master | sed 's/origin\///' | xargs -n 1 git push --delete origin
    

1 个答案:

答案 0 :(得分:3)

您可以使用shell脚本删除早于一年的合并分支,并删除超过五个月的合并分支。

删除早于一年的未合并分支

#!/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

删除超过五个月的合并分支

#!/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