I'm looking for the best way to execute a function for each Git remote branch in a PowerShell script.
I don't know the best way to get a list of Git remote branches. I can successfully list all remote branches, but I always end up with pesky formatting in the branch names.
Here are the commands I've tried and the resulting arrays I've gotten.
$allBranches = git branch -r
$allBranches = @(' origin/branch1', ' origin/branch2', ' origin/branch3')
$allBranches = git for-each-ref --shell --format=%(refname) refs/remotes/origin
$allBranches = @(''origin/branch1'', ''origin/branch2'', ''origin/branch3'')
I would like
$allBranches = @('origin/branch1', 'origin/branch2', 'origin/branch3')
, so my current approach is to just manually remove formatting from the weird branch names using Trim()
:
foreach($branch in $allBranches) {
# Format $branch
# Do function
}
Is there a better approach?
The Trim() operation should do what you want.
$allTrimmedBranches = @()
foreach($branch in $allBranches) {
$allTrimmedBranches += $branch.Trim()
}
#do function