Skip to content

Instantly share code, notes, and snippets.

@manoj-choudhari-git
Last active September 19, 2023 23:29

Revisions

  1. manoj-choudhari-git revised this gist Sep 19, 2023. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions delete-stale-branches.ps1
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,7 @@
    ################################################################

    ## Parameter is_dry_run, defaulted to true to avoid deletion by mistake
    ## Invoke Command - "$ delete-stale-branches.ps1 -is_dry_run $true"
    param([Boolean]$is_dry_run = $true)

    ##---------------------------------------------------------------
  2. manoj-choudhari-git revised this gist Sep 19, 2023. 1 changed file with 24 additions and 5 deletions.
    29 changes: 24 additions & 5 deletions delete-stale-branches.ps1
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,8 @@
    ## from the machine where the remote repository is cloned.
    ################################################################

    ## Parameter is_dry_run, defaulted to true to avoid deletion by mistake
    param([Boolean]$is_dry_run = $true)

    ##---------------------------------------------------------------
    ## List of Main Branches (Which Should Not Be Deleted)
    @@ -16,16 +18,24 @@ $defaultBranch = git symbolic-ref refs/remotes/origin/HEAD --short
    write-host "Default Branch In Repo: $defaultBranch"
    if ($excludedBranchNames -inotcontains $defaultBranch)
    {
    $excludedBranchNames.Add($defaultBranch);
    $excludedBranchNames += $defaultBranch
    }

    ##---------------------------------------------------------------
    ## Cleanup of Stale Branches - No Commits since 3 Months
    ## Prune Local References Of Remote Branches
    ##---------------------------------------------------------------

    ## Just to avoid error due to non-existing remote branch
    ## prune local list
    git fetch --prune

    ##---------------------------------------------------------------
    ## Cleanup of Stale Branches - No Commits since 3 Months
    ##---------------------------------------------------------------

    ## List all remote branches
    $remoteBranches = git branch -r
    $remoteBranches = git branch -r


    ## Explicitly Exclude HEAD
    $remoteBranchesFiltered = $remoteBranches | Where { $_ -notlike '*HEAD*'};
    @@ -45,10 +55,19 @@ foreach ($branch in $remoteBranchesFiltered)
    ## If not found, delete the branch
    if (!$lastCommitDate)
    {

    Write-Host "Deleting branch $branchName"

    ## For dry run comment this line
    git branch -D origin/$branchName
    if ($is_dry_run -eq $false )
    {
    ## --porcelain is used so that git command writes script friendly output
    git push origin -d $branchName --porcelain
    }
    }
    }
    }

    ##---------------------------------------------------------------
    ## Prune Local References Again
    ##---------------------------------------------------------------
    git fetch --prune
  3. manoj-choudhari-git revised this gist Sep 19, 2023. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions delete-stale-branches.ps1
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,9 @@
    ################################################################
    ## Note: This script should be executed
    ## from the machine where the remote repository is cloned.
    ################################################################


    ##---------------------------------------------------------------
    ## List of Main Branches (Which Should Not Be Deleted)
    ##---------------------------------------------------------------
  4. manoj-choudhari-git created this gist Sep 19, 2023.
    48 changes: 48 additions & 0 deletions delete-stale-branches.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    ##---------------------------------------------------------------
    ## List of Main Branches (Which Should Not Be Deleted)
    ##---------------------------------------------------------------

    ## Exclusion List - Add general default branch names
    $excludedBranchNames = @('main', 'master', 'develop')

    ## Just in case if there is any other name of default branch, add that too in exclusion list
    $defaultBranch = git symbolic-ref refs/remotes/origin/HEAD --short
    write-host "Default Branch In Repo: $defaultBranch"
    if ($excludedBranchNames -inotcontains $defaultBranch)
    {
    $excludedBranchNames.Add($defaultBranch);
    }

    ##---------------------------------------------------------------
    ## Cleanup of Stale Branches - No Commits since 3 Months
    ##---------------------------------------------------------------


    ## List all remote branches
    $remoteBranches = git branch -r

    ## Explicitly Exclude HEAD
    $remoteBranchesFiltered = $remoteBranches | Where { $_ -notlike '*HEAD*'};

    ## Loop through each branch
    foreach ($branch in $remoteBranchesFiltered)
    {
    ## Remove the origin/ prefix
    $branchName = $branch.Trim().Substring(7)

    ## Continue deletion only if Branch name is not present in exclusion list
    if ( $excludedBranchNames -inotcontains $branchName)
    {
    ## Try to find at least 1 commit in last 3 months
    $lastCommitDate = git log -1 --since="3 months ago" --format="%cd" origin/$branchName

    ## If not found, delete the branch
    if (!$lastCommitDate)
    {
    Write-Host "Deleting branch $branchName"

    ## For dry run comment this line
    git branch -D origin/$branchName
    }
    }
    }