Skip to content

Instantly share code, notes, and snippets.

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 RickyLin/4f38484664f39644862ad9db6e60500e to your computer and use it in GitHub Desktop.
Save RickyLin/4f38484664f39644862ad9db6e60500e to your computer and use it in GitHub Desktop.
Delete local branches whose remote tracking branches were gone.
git fetch origin -np
$BranchesToDelete = New-Object -TypeName System.Collections.Generic.List[string]
$BranchList = (git branch -vv)
foreach ($Branch in $BranchList) {
if ($Branch.ToString().Contains(": gone]")) {
$BranchesToDelete.Add($Branch.ToString().TrimStart().Split(' ')[0])
}
}
if ($BranchesToDelete.Count -eq 0) {
"No local branch needs to be deleted."
return;
}
Write-Host "The following local branch(es) will be deleted:" -ForegroundColor Yellow
foreach ($BranchName in $BranchesToDelete) {
Write-Host ("`t" + $BranchName) -ForegroundColor Red
}
$Confirmation = Read-Host "Do you want to delete local branches above? (Y/n)"
# the -eq 'y' is case-insensitive, so it's also true if $Confirmation is 'Y'
if (($Confirmation -eq "y") -or ($Confirmation -eq "")) {
foreach ($BranchName in $BranchesToDelete) {
git branch -D $BranchName
}
Write-Host "Done." -ForegroundColor Green
}
else {
"No branch was deleted."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment