Created
March 22, 2021 16:52
-
-
Save kilasuit/0767316bcfe14d3088957ae14b93ddf8 to your computer and use it in GitHub Desktop.
PowerShell Function to clean your local git branches (inspired by https://blogs.blackmarble.co.uk/rfennell/2021/03/16/tidying-up-local-branches-with-a-git-alias-and-a-powershell-script/ )
This file contains hidden or 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
| 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