I’m pretty sure you have tons of Git branches that you have merged and removed on your server, but are still on your local clone. Here’s a Powershell script to clean them up, using non-brittle commands (just doing git branch -vv
like you often see is not a good practice).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
<# .DESCRIPTION Checks that the specified folder is a Git folder (simple check of a .git sub-folder) #> function Test-GitFolder { param ( [string]$folder ) return (Test-Path "$folder\.git") } <# .DESCRIPTION Gets the Git folders in the current repository #> function Get-GitFolders { $result = @() $folders = Get-ChildItem "." -Directory foreach ($folder in $folders) { if (!(Test-GitFolder $folder)) { continue } $result += $folder } return $result } <# .DESCRIPTION Removes the outdated branches on a series of repositories #> function Remove-OutdatedBranches { param ( $path = "." ) if (!(Test-GitFolder $path)) { Write-Host "Current folder $path not a Git folder" -ForegroundColor Red return } Write-Host "Removing stale branches from $path" -ForegroundColor Magenta # fetch remote and prune & git -C "$path" fetch --prune # list branches and their status $branches = & git -C "$path" branch --format "%(refname:short)|%(upstream:track)" foreach ($branch in $branches) { $b = $branch.Split("|") if ($b.Length -gt 2 -or $b[1] -eq "") { continue } if ($b[1].Contains("[gone]")) { # remove branch & git -C "$path" branch -D $b[0] } } } <# .DESCRIPTION Removes outdated branches in all the subfolders of the current folder #> function Remove-AllOutdatedBranches { $folders = Get-GitFolders foreach ($folder in $folders) { Remove-OutdatedBranches $folder } } |