24

How do I interpret the errors coming out of this PowerShell script that calls "Git Clone" (actually using GitLab). And can I clone an empty directory and cause it to work without getting errors?

$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

Output:

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.

In the larger script where I want to use this code, I include this:

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

so that it stops on the first error.

Even when I run it on a non-empty project, I see red and errors similar to above (took the --local and --verbose off this run):

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

If I run from windows command prompt, it runs nicely with some stats:

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.
1
  • 1
    I'm experimenting with the --Quiet flag, which might work, but not sure why I'm getting any errors that need to be suppressed with that flag. Commented Jan 25, 2019 at 21:00

1 Answer 1

15
+50

As I mention in "PowerShell Capture Git Output", since a Git command can output information message on stderr (instead of stdout), (for Git 2.16+) try this in the command prompt

set GIT_REDIRECT_STDERR=2>&1

or this command for powershell

$env:GIT_REDIRECT_STDERR = '2>&1'

That might avoid your script to stop on the first "error", which is actually not an error

The OP NealWalters adds in the comments:

I ended up using the wrapper function Invoke-Git from "Git clone: Redirect stderr to stdout but keep errors being written to stderr"

Sign up to request clarification or add additional context in comments.

4 Comments

This is an environmental variable? From Win command prompt I tried: set GIT_REDIRECT_STDERR=2>&1, then typed in set and didn't see the variable created. I also tried in Powershell, it says: + set GIT_REDIRECT_STDERR=2>&1 + ~ The ampersand (&) character is not allowed. The & operator is reserved for future use; wrap an ampersand in double quotation marks ("&") to pass it as part of a string. My Git Version: 2.16.1.windows.1
@NealWalters Yes it is: mode details in stackoverflow.com/a/47232450/6309 and github.com/git/git/commit/…: GIT_REDIRECT_STDERR="2>&1"
I ended up using the wrapper function Invoke-Git from the link above: stackoverflow.com/questions/34820975/…
@NealWalters Great! I have included your comment in the answer for more visibility.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.