Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script to cleanup merged branches locally
[alias]
cb = "!f() { git branch --merged | grep -v "master" | while read i; do git branch -d $i; done; }; f"
function Cleanup-GitBranches()
{
$branches = git branch --merged | ?{$_ -notmatch "\* master"} | ?{$_ -notmatch "master"} | ?{$_ -notmatch "\* *"} | %{$_.Trim() }
if (-not $branches) {
echo "No merged branches detected"
exit 0
}
echo $branches
$title = "Delete Merged Branches"
$message = "Do you want to delete the already-merged local branches displayed above??"
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", `
"Delete the remote branches listed."
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", `
"Leave the branches alone."
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$result = $host.ui.PromptForChoice($title, $message, $options, 1)
if ($result -eq 1) {
exit 0
}
$branches | %{ git branch -d "$_" }
}
@aaronpowell

This comment has been minimized.

Copy link
Owner Author

commented Jul 23, 2012

Drop that into a known location (such as %Documents%\WindowsPowerShell) and add this to get it enabled for all Powershell sessions:

. 'C:\Users\<username>\Documents\WindowsPowerShell\Cleanup-GitBranches.ps1'
@aaronpowell

This comment has been minimized.

Copy link
Owner Author

commented Apr 30, 2014

Added a git alias as well for if using bash

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.