Created
August 15, 2026 00:31
-
-
Save jongalloway/8e983bf0d761826f8deacf84e2660c07 to your computer and use it in GitHub Desktop.
Install git-cleanup in your PowerShell profile: iex (irm 'https://gist.githubusercontent.com/jongalloway/8e983bf0d761826f8deacf84e2660c07/raw/install-git-cleanup.ps1')
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
| $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