Skip to content

Instantly share code, notes, and snippets.

@manoj-choudhari-git
Last active September 19, 2023 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manoj-choudhari-git/b485c23c3670f8ac2180a1ded44584fa to your computer and use it in GitHub Desktop.
Save manoj-choudhari-git/b485c23c3670f8ac2180a1ded44584fa to your computer and use it in GitHub Desktop.
PowerShell Script - To Automate Clean up of Stale Branches From Remote Repository
################################################################
## Note: This script should be executed
## from the machine where the remote repository is cloned.
################################################################
## 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)
##---------------------------------------------------------------
## 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 += $defaultBranch
}
##---------------------------------------------------------------
## 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
## 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"
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment