Last active
July 29, 2023 10:08
-
-
Save rfennell/281e7e6c34d5ab511b2a4b38b8ceae72 to your computer and use it in GitHub Desktop.
A PowerShell script to do a git fetch with a prune then also delete any local branches of the same name
This file contains 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
# To set as Git Alias | |
# git config --global alias.tidy "!pwsh -command C:\MyFolder\Remove-DeletedGitBranches.ps1" | |
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) | |
{ | |
Write-Host "Deleting all removed branched" | |
$branches | ForEach-Object {git branch -D $_}; | |
} else { | |
Write-Host "Deleting removed branched if committed" | |
$branches | ForEach-Object {git branch -d $_}; | |
} | |
Write-Host "Fetching changes" | |
git fetch |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To make this a git alias use the command