Skip to content

Instantly share code, notes, and snippets.

@pkutaj
Created September 16, 2021 06:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pkutaj/0f6a2ed0abb444bd7415685c2505f34b to your computer and use it in GitHub Desktop.
Save pkutaj/0f6a2ed0abb444bd7415685c2505f34b to your computer and use it in GitHub Desktop.
Show Git Branches Which are local-only, remote-only or both
function match-branch ([switch]$local_only, [switch]$remote_only, [switch]$both) {
$localBranches = ((git branch -l) -replace "\*", "") -replace " ", ""
$remoteBranches = (((git branch -r) -replace "\*", "") -replace " ", "") -replace "origin/", ""
$branch_comparison = Compare-Object -ReferenceObject $localBranches -DifferenceObject $remoteBranches -IncludeEqual
| Select-Object @{Label = "branch"; Expression = { $_.InputObject } },
@{Label = ”both”; Expression = { $_.SideIndicator -eq "==" } },
@{Label = ”remote_only”; Expression = { $_.SideIndicator -eq "=>" } },
@{Label = ”local_only”; Expression = { $_.SideIndicator -eq "<=" } }
if ($local_only) { $branch_comparison | Where-Object -Property "local_only" -EQ "true" | Select-Object branch }
elseif ($remote_only) {$branch_comparison | Where-Object -Property "remote_only" -EQ "true" | Select-Object branch}
elseif ($both) { $branch_comparison | Where-Object -Property "both" -EQ "true" | Select-Object branch }
else {$branch_comparison}
<#
.SYNOPSIS
Function displaying which git branches are local-only, remote-only and both local and remote
Called without a switch returns a table
To filter the table, use switches -local_only; -remote_only; -both
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment