Skip to content

Instantly share code, notes, and snippets.

@Absolucy
Created April 3, 2024 23:03
Show Gist options
  • Save Absolucy/20eb5d899e80dd8ec0f09588c9c14c8b to your computer and use it in GitHub Desktop.
Save Absolucy/20eb5d899e80dd8ec0f09588c9c14c8b to your computer and use it in GitHub Desktop.
delete local git branches that were deleted remotely
function Remove-Unused-Branches {
$refsToDelete = git for-each-ref --format '%(refname:short) %(upstream:track)' |
ForEach-Object {
$ref, $upstream = $_ -split '\s+'
if ($upstream -eq '[gone]') {
Write-Output $ref
}
}
Write-Host "Following refs will be deleted:"
$refsToDelete
Write-Host "Do you want to continue and delete these refs? (Y/N)"
$userInput = Read-Host
if ($userInput -eq 'Y') {
$refsToDelete | ForEach-Object {
git branch -D $_
}
} else {
Write-Host "Skipping deletion of refs"
}
}
$alias:cleanup = "Remove-Unused-Branches"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment