Created
April 3, 2024 23:03
-
-
Save Absolucy/20eb5d899e80dd8ec0f09588c9c14c8b to your computer and use it in GitHub Desktop.
delete local git branches that were deleted remotely
This file contains hidden or 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 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