来自 Powershell 的 运行 "Git-Clone" 即使它似乎有效也会出错

Running "Git-Clone" from Powershell giving errors even when it seems to work

如何解释调用“Git 克隆”(实际上使用 GitLab)的 PowerShell 脚本中出现的错误。我可以克隆一个空目录并使其正常工作吗?

$path1 = "d:\GitPath" 
$path2 = "${path1}\mycompany-mygroup-nealtestautomation01-map"
$gitLabHttp = "http://git.mycompany.com/MyGroup/nealtestscriptaddedproject.git"
$gitLabHttp = "http://git.mycompany.com/MyGroup/mycompany-gwcustomers-nealtestautomation01-map.git" 
rd $path2 
cd $path1
git clone $gitLabHttp --local --verbose

输出:

git : Cloning into 'mycompany-mygroup-nealtestautomation01210-map'...
At D:\Scripts\GitCloneTest.ps1:7 char:1
+ git clone $gitLabHttp --local --verbose
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Cloning into '...mation01-map'...:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
 
warning: --local is ignored
warning: You appear to have cloned an empty repository.

在我要使用此代码的较大脚本中,我包含了以下内容:

$ErrorActionPreference = "Stop"  #Special Poweshell Syntax to Stop on First Error 

以便它在出现第一个错误时停止。

即使我 运行 它在一个非空项目上,我也看到红色和类似于上面的错误(取消了这个 运行 的 --local 和 --verbose):

git : Cloning into 'xxx.yyy.CanInvToOutZZZ210.Map'...
At D:\Scripts\GitCloneTest2.ps1:5 char:1
+ git clone $gitLabHttp
+ ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Cloning into 'E...Intl210.Map'...:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

如果我在 windows 命令提示符下 运行,它 运行 与一些统计数据相得益彰:

D:\GitGWCustomerMaps\Test2>git clone myrealurlhidden 
Cloning into 'XXX.YYY.CanInvToOutZZZZ210.Map'...
remote: Counting objects: 29, done.
remote: Compressing objects: 100% (28/28), done.
remote: Total 29 (delta 9), reused 0 (delta 0)
Unpacking objects: 100% (29/29), done.

正如我在“", since a Git command can output information message on stderr (”中提到的,尝试(使用 Gti 2.16+)

set GIT_REDIRECT_STDERR=2>&1

(或使该变量设置适应您的脚本)

这可能会避免您的脚本在第一个 "error" 时停止,这实际上不是错误

OP NealWalters adds :

I ended up using the wrapper function Invoke-Git from ""