Skip to content

Instantly share code, notes, and snippets.

@kilasuit
Created March 22, 2021 16:52
Show Gist options
  • Save kilasuit/0767316bcfe14d3088957ae14b93ddf8 to your computer and use it in GitHub Desktop.
Save kilasuit/0767316bcfe14d3088957ae14b93ddf8 to your computer and use it in GitHub Desktop.
function gitcleanbranches {
[CmdletBinding()]
[alias('gitcb')]
param(
[switch]
[alias('n')]
$Notmerged,
[switch]
[alias('f')]
$force
)
$output = [string] (& git.exe branch 2>&1)
$mainbranch = "main"
if ($output.Contains("master"))
{
$mainbranch = "master"
}
git checkout $mainbranch
$null = (git fetch --all --prune);
if ($notmerged) {
git for-each-ref --format '%(refname:short)' refs/heads | ForEach-Object { if ($_ -notmatch 'master|main') {
if ($force) { git branch $_ -D }
else { git branch $_ -d }
}
}
}
else {
git for-each-ref --format '%(refname:short)' refs/heads --merged | ForEach-Object { if ($_ -notmatch 'master|main') {
if ($force) { git branch $_ -D }
else { git branch $_ -d }
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment