【问题标题】:Git list of remote branchesGit远程分支列表
【发布时间】:2015-10-31 02:38:39
【问题描述】:

我正在寻找在 PowerShell 脚本中为每个 Git 远程分支执行函数的最佳方法。

我不知道获取 Git 远程分支列表的最佳方法。我可以成功列出所有远程分支,但我总是在分支名称中出现讨厌的格式。

这是我尝试过的命令以及我得到的结果数组。

$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'')

我想要 $allBranches = @('origin/branch1', 'origin/branch2', 'origin/branch3'),所以我目前的方法是使用 Trim() 从奇怪的分支名称中手动删除格式:

foreach($branch in $allBranches) {
    # Format $branch
    # Do function
}

有更好的方法吗?

【问题讨论】:

  • stackoverflow.com/questions/3846380/… 标题中有 Bash,但确实包含您想要的 Git 魔法
  • 您是在寻找 Powershell 代码来轻松格式化返回的分支名称,还是寻找不包含额外空格的更好的 git 调用?
  • 我正在寻找 Powershell 脚本,而不是 Bash 脚本。我知道如何格式化分支名称。我想要一个更好的 git 调用或更好的方法来遍历分支。

标签: git powershell foreach git-branch


【解决方案1】:

$branches = git for-each-ref --format='%(refname:short)' refs/remotes/origin

【讨论】:

  • 请在您的回答中添加一些解释和/或描述。
【解决方案2】:

Trim() 操作应该做你想做的。

$allTrimmedBranches = @()
foreach($branch in $allBranches) {
    $allTrimmedBranches += $branch.Trim()
}
#do function

【讨论】:

  • 我目前的方法是修剪分支名称,这似乎效率很低。我想要一个更好的 git 调用或更好的方法来遍历分支。
  • 你有多少家分店?修剪一百个或更少的字符串所需的时间/资源可以忽略不计。
  • 我有
猜你喜欢
  • 2012-10-22
  • 2012-04-21
  • 2016-07-21
  • 2012-03-21
  • 2013-11-21
  • 2014-03-17
  • 2014-04-27
相关资源
最近更新 更多