Clear deleted git branches
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Clear-DeletedBranches { | |
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')] | |
[Alias("cdb")] | |
param( | |
[Parameter(Mandatory = $false)] | |
[string] | |
$GitDir = $PWD | |
) | |
$defaultBranch = ((git symbolic-ref --short refs/remotes/origin/HEAD) -split "/")[1] | |
Write-Host "Switching to branch [$defaultBranch]..." -ForegroundColor Yellow | |
git checkout $defaultBranch | |
$branchesBefore = git branch -a | |
$branchesToPrune = git remote prune origin --dry-run | |
if ($branchesToPrune) { | |
Write-Host "Branches to Be Pruned..." -ForegroundColor Green | |
Write-Host $branchesToPrune -ForegroundColor Red | |
if ($PSCmdlet.ShouldProcess("Remove Local Branches?")) { | |
git remote prune origin | |
$branchesAfter = git branch -a | |
$removed = Compare-Object -ReferenceObject $branchesBefore -DifferenceObject $branchesAfter | |
foreach ($b in $removed.InputObject) { | |
$localName = $b.Split('/')[-1] | |
git branch -D $localName | |
} | |
} | |
} else { | |
Write-Host "Nothing To prune" -ForegroundColor Green | |
} | |
Write-Host "`nPulling changes from default branch..." -ForegroundColor Green | |
git pull | |
Write-Host "`nCurrent branches..." -ForegroundColor Green | |
git branch -a | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment