VSTS:如何自动删除git分支

VSTS:如何自动删除git分支,git,azure-devops,Git,Azure Devops,目前,我们的开发团队使用的是VST和git。分支的数量越来越多,我希望能够自动删除那些已经有60天没有被触碰过的分支,而不是每个月手动删除分支。我知道这在Jenkins中是可行的,但我们目前还没有Jenkins,我想知道这是否可以通过一些服务钩子在VSTS中实现?我建议您设置一个计划的构建定义,该定义将运行git命令,以找出需要删除的内容,并删除服务器上的这些分支 有用资源: VSTS是。但是有其他方法可以自动删除本地机器中的旧分支。具体步骤如下: git fetch origin for

目前,我们的开发团队使用的是VST和git。分支的数量越来越多,我希望能够自动删除那些已经有60天没有被触碰过的分支,而不是每个月手动删除分支。我知道这在Jenkins中是可行的,但我们目前还没有Jenkins,我想知道这是否可以通过一些服务钩子在VSTS中实现?

我建议您设置一个计划的构建定义,该定义将运行git命令,以找出需要删除的内容,并删除服务器上的这些分支

有用资源:

  • VSTS是。但是有其他方法可以自动删除本地机器中的旧分支。具体步骤如下:

    git fetch origin
    for reBranch in $(git branch -a)
    do
    {
      if [[ $reBranch == remotes/origin* ]];
      then
      {
        if [[ $reBranch ==remotes/origin/HEAD ]]; then 
        echo "HEAD is not a branch"
        else
          branch=$(echo $reBranch | cut -d'/' -f 3)
          echo $branch
          sha=$(git rev-parse origin/$branch)
          dateo=$(git show -s --format=%ci $sha)
          datef=$(echo $dateo | cut -d' ' -f 1)
          Todate=$(date -d "$datef" +'%s')
          current=$(date +'%s')
          day=$(( ( $current - $Todate )/60/60/24 ))
          echo $day
          if [ "$day" -gt 180 ]; then
          git push origin :$branch
          echo "delete the old branch $branch"
          fi
        fi
    
      }
      fi
    }
    done
    
    1.在某个目录中(例如
    D:\script\u for\u git
    ),克隆VSTS git repo(仅用于自动删除分支)

    2.在根git repo(
    D:\script\u for_git\repo
    )中添加一个shell脚本(
    del.sh
    ),以删除180天前(6个月)未更改的远程分支,shell脚本的内容如下:

    git fetch origin
    for reBranch in $(git branch -a)
    do
    {
      if [[ $reBranch == remotes/origin* ]];
      then
      {
        if [[ $reBranch ==remotes/origin/HEAD ]]; then 
        echo "HEAD is not a branch"
        else
          branch=$(echo $reBranch | cut -d'/' -f 3)
          echo $branch
          sha=$(git rev-parse origin/$branch)
          dateo=$(git show -s --format=%ci $sha)
          datef=$(echo $dateo | cut -d' ' -f 1)
          Todate=$(date -d "$datef" +'%s')
          current=$(date +'%s')
          day=$(( ( $current - $Todate )/60/60/24 ))
          echo $day
          if [ "$day" -gt 180 ]; then
          git push origin :$branch
          echo "delete the old branch $branch"
          fi
        fi
    
      }
      fi
    }
    done
    

    3。计划运行此shell脚本。有许多方法可以安排运行脚本,并且与操作系统相关。例如,如果您使用的是windows,则可以参考。如果您使用的是linux,您可以参考。

    在合并拉请求时,有一个选项可以自动删除分支,这是一个解决方案吗?让我问一下——我看到团队在清理分支之前会离开分支几个月。这样做的原因是什么?如果没有,那么我不明白为什么这不能成为一个解决方案。我不认为有任何理由保留旧分支。尤其是打算在几个月内把它们搬走。在我的所有项目中,我们在合并后删除分支。