Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A PowerShell script to do a git fetch with a prune then also delete any local branches of the same name
param
(
[Parameter()]
[Switch]
$Force
)
Write-Host "Getting list of branches"
$output = [string] (& git.exe branch 2>&1)
$mainbranch = "main"
if ($output.Contains("master"))
{
$mainbranch = "master"
}
Write-Host "Trunk branch is $mainbranch"
git checkout $mainbranch
$null = (git fetch --all --prune);
$branches = git branch -vv | Select-String -Pattern ": gone]" | ForEach-Object { $_.toString().Split(" ")[2] };
if($Force)
{
$branches | ForEach-Object {git branch -D $_};
} else {
$branches | ForEach-Object {git branch -d $_};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment