Skip to content

Instantly share code, notes, and snippets.

@kilasuit
Created March 16, 2021 14:58
Show Gist options
  • Save kilasuit/cb055e19bef793d07486a5d8255ed61d to your computer and use it in GitHub Desktop.
Save kilasuit/cb055e19bef793d07486a5d8255ed61d to your computer and use it in GitHub Desktop.
Functions for cleaning up local git branches
function gitforcecleanbranches {
[CmdletBinding()]
[alias('gitcbf')]
param(
[switch]
$Notmerged
)
if ($notmerged) {
git for-each-ref --format '%(refname:short)' refs/heads | ForEach-Object { if ($_ -match 'master|main') { git branch $_ -D } }
}
else {
git for-each-ref --format '%(refname:short)' refs/heads --merged | ForEach-Object { if ($_ -match 'master|main') { git branch $_ -D } }
}
}
function gitcleanbranches {
[CmdletBinding()]
[alias('gitcb')]
param(
[switch]
$Notmerged
)
if ($notmerged) {
git for-each-ref --format '%(refname:short)' refs/heads | ForEach-Object { if ($_ -match 'master|main') { git branch $_ -d } }
}
else {
git for-each-ref --format '%(refname:short)' refs/heads --merged | ForEach-Object { if ($_ -match 'master|main') { git branch $_ -d } }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment