Created
August 15, 2019 02:16
-
-
Save RickyLin/4f38484664f39644862ad9db6e60500e to your computer and use it in GitHub Desktop.
Delete local branches whose remote tracking branches were gone.
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
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