Skip to content

Instantly share code, notes, and snippets.

@jeppevammenkristensen
Created March 16, 2022 09:25
Show Gist options
  • Save jeppevammenkristensen/d186bb7d750d3331fb752a0a30b77658 to your computer and use it in GitHub Desktop.
Save jeppevammenkristensen/d186bb7d750d3331fb752a0a30b77658 to your computer and use it in GitHub Desktop.
Delete orphan branches Git
# A minor modification of the script here https://www.mehmetseckin.com/posts/clean-up-git-branches-that-do-not-exist-on-origin/
function Remove-DeletedGitBranches
{
$null = (git fetch --all --prune);
$branches = git branch -vv | Select-String -Pattern ": gone]" | ForEach-Object { $_.toString().Split(" ")[2] };
if ($branches.Count -gt 0)
{
Write-Host "Found the following orphan branches: " -ForegroundColor Green
$branches
$response = Read-Host "Delete orphan branches? (y)";
if ($response -eq 'y')
{
$branches | ForEach-Object {git branch -D $_};
}
}
else
{
Write-Host "No branches found that was deleted remote";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment