Created
March 16, 2022 09:25
-
-
Save jeppevammenkristensen/d186bb7d750d3331fb752a0a30b77658 to your computer and use it in GitHub Desktop.
Delete orphan branches Git
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
# 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