Skip to content

Instantly share code, notes, and snippets.

@jongalloway
Created August 15, 2026 00:31
Show Gist options
  • Select an option

  • Save jongalloway/8e983bf0d761826f8deacf84e2660c07 to your computer and use it in GitHub Desktop.

Select an option

Save jongalloway/8e983bf0d761826f8deacf84e2660c07 to your computer and use it in GitHub Desktop.
$profilePath = $PROFILE.CurrentUserCurrentHost
$functionPattern = '(?m)^\s*function\s+git-cleanup\b'
if (Test-Path -LiteralPath $profilePath) {
$profileContent = Get-Content -LiteralPath $profilePath -Raw
if ($profileContent -match $functionPattern) {
Write-Host "git-cleanup is already defined in $profilePath"
return
}
}
$profileDirectory = Split-Path -Parent $profilePath
if (-not (Test-Path -LiteralPath $profileDirectory)) {
New-Item -ItemType Directory -Path $profileDirectory -Force | Out-Null
}
$gitCleanupFunction = @'
# Removes local branches whose remote tracking branch has been deleted.
function git-cleanup {
git fetch --prune
git branch -vv | ForEach-Object {
if ($_ -match '^\s+(\S+)\s+\S+\s+\[.+: gone\]') {
git branch -D $matches[1]
}
}
}
'@
[System.IO.File]::AppendAllText(
$profilePath,
$gitCleanupFunction,
[System.Text.UTF8Encoding]::new($false)
)
Invoke-Expression $gitCleanupFunction
Write-Host "Added git-cleanup to $profilePath"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment