英文:
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.
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.
- The PowerShell ISE is no longer actively developed and there are reasons not to use it (bottom section), notably not being able to run PowerShell (Core) 7+. The actively developed, cross-platform editor that offers the best PowerShell development experience is Visual Studio Code with its PowerShell extension.
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.
- In the case at hand,
-
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 theErrorRecord
instances that represent the stderr output to strings with% ToString
(%
is a built-in alias of theForEach-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).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论