如何使用 Powershell 正确克隆 Github 存储库分支

huangapple go评论69阅读模式
英文:

How to correctly clone a Github repo branch using Powershell

问题

克隆存储库时出现的错误可能是由于权限问题引起的。建议以管理员权限运行脚本,以避免此错误。

英文:

I'm trying to clone a Github repo.

Function CloneRepo{
    $solnFolder = -join($outputDir,"\", $newSolutionName)
    git clone -b master "https://github.com/sukesh-y/Stratum.git" $solnFolder
}

$newSolutionName = "MyCompany"
$outputDir = "D:\Test"
CloneRepo

This does clone the repo but there is an error in console.
When I execute the script, it throws the error. After many seconds, it shows the Uploading files progress.

如何使用 Powershell 正确克隆 Github 存储库分支

如何使用 Powershell 正确克隆 Github 存储库分支

I'm running Windows PowerShell ISE with Admin privileges.
What is this error and how can I avoid it.

答案1

得分: 2

简而言之:

PowerShell ISE 对于运行外部程序(如 git)不适用,因为它不适当地将 stderr 输出显示为 PowerShell 错误,而且总体上缺乏正确的终端支持。这些问题在常规控制台窗口、Windows 终端或 Visual Studio Code 集成终端中不存在。建议迁移到 Visual Studio Code,以避免问题。

你好,有其他需要帮助的吗?

英文:

tl;dr

  • The obsolescent PowerShell ISE is ill-suited for running external programs such as git: it inappropriately surfaces stderr output as PowerShell errors and lacks proper terminal support in general.

    • Fortunately, these problems do not exist in regular console windows, Windows Terminal, or in Visual Studio Code's integrated terminal.

    • However, even there stderr lines surface and print as PowerShell errors in the output from remoting calls, and you can use the techniques below to prevent that. The unexpected as-error formatting is less problematic when calling from PowerShell (Core) 7+, because only the stderr line text prints in red, whereas in Windows PowerShell you get the same "noisy", multi-line display for the first stderr line as in the ISE. Also, the $ErrorActionPreference = 'Stop' caveat discussed below applies to remoting too.

  • While you can situationally - suboptimally - work around the problem (see below), you're better off migrating to Visual Studio Code with its PowerShell extension.


Your screenshot implies that you're using the PowerShell ISE.

Among the other reasons not to use it is the case at hand:

  • The ISE inappropriately surfaces stderr output from external programs as if they were PowerShell errors, which is undesired and confusing

    • The fact that the spurious errors surface as RemoteException adds to the confusion.

    • More importantly, if $ErrorActionPreference = 'Stop' is in effect, stderr output will inappropriately abort your script: you can not infer failure of an external-program call from the presence of stderr output; only the exit code matters, which must be checked via automatic $LASTEXITCODE variable.

  • Since external programs only have two output streams at their disposal - stdout and stderr - stderr is used for anything that is not data, which isn't necessarily just actual error messages, but often status information.

    • In the case at hand, git uses stderr to provide status information in the form of an iteratively updated progress display meant to update in place.
    • Because the ISE lacks proper terminal support, each progress update ends up on a separate line.
  • If migrating to Visual Studio Code is not an option, you have three options to deal with the problem, all of them suboptimal:

    • You can ignore the problem, and put up with the visual disruption.

    • You can silence stderr output with 2>$null - which means you'll lose not only the status information, but potentially error messages too:

      # Suppress all stderr output
      git ... 2>$null
      
    • You can merge stderr into PowerShell's success output stream with 2>&1, and convert the ErrorRecord instances that represent the stderr output to strings with % ToString (% is a built-in alias of the ForEach-Object cmdlet):

      # Merge stderr output into the success output stream and stringify.
      # Note: If you *capture* the results, you'll get the stdout and
      #       stderr *combined*.
      git ... 2>&1 | % ToString
      
  • None of these workarounds work well in your particular case (progress display), because the information doesn't print until after the command has finished (except that with the first option the first, verbose error message prints right away).

huangapple
  • 本文由 发表于 2023年4月6日 19:27:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75948982.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定