14

When I execute Git commands in PowerShell everything is working great except one minor difference between the PowerShell console and the NuGet console. The output from a "git push" is displayed in red error text in the NuGet window but displays fine in the PowerShell window.

Here is a video showing the difference. "git push" displays its results as an error in the package manager console. The operation worked, it's just annoying that the output from the operation is displayed as an error.

See the video

wonea
  • 4,783
  • 17
  • 86
  • 139
CatDadCode
  • 58,507
  • 61
  • 212
  • 318

2 Answers2

30

Root cause: git push is sending output to stderr, not stdout. See here, here.

Powershell.exe as a host does not get worried when native tools send output to stderr, since this is a somewhat common way to print not just error messages but status messages and other stuff. For example, try running something totally bogus like

PS C:\> $result = findstr.exe q w e r t y

Findstr is sending error messages to stderr, so Powershell.exe knows not to assign that "error" output to your variable, but it also doesn't freak out.

The NuGet package manager host, on the other hand, is not as smart in this regard. When running any native tool, this host interprets anything in stderr as being truly an error. Thus you get the red text, diagnostic messages, etc. Try the same findstr example above in the PM, you will see full errors.

A couple of workarounds/suggestions:

  • Use the --porcelain parameter, which causes output to go to stdout, not stderr.
  • Set $errorView = 'CategoryView' which will at least minimize the error messages, though not remove them
  • Redirect stderr and do a plain console write like this: git push 2>&1 | write-host
latkin
  • 16,402
  • 1
  • 47
  • 62
  • 2
    The link for "stdout, not stderr." seems "broken". For Windows, `--porcelain` is an unknown option. But piping the output to Write-Host works! Thank you! :) – Rami A. May 14 '16 at 12:47
  • `--porcelain` also seems to be an unknown option for me also on git version 2.12.0.windows.1 :( – Luke Apr 11 '17 at 15:35
  • a quote from that first link... `In general, any program or script is buggy if it assumes that some output that are emitted to the standard error output from programs it invokes indicates an error` ... SMH – StingyJack Oct 20 '18 at 13:55
  • The nabble.com link is, of course, dead after so many years. Please use authoritative sources of information (i.e. https://git-scm.com/) ***and*** include the pertinent text in the answer. – Mike Nakis Sep 12 '21 at 16:05
4

To fix this and keep working with Powershell ISE you can create a cmd file with the following content:

@echo off
git.exe %1 %2 %3 %4 %5 %6 %7 %8 %9 2>&1

And register it as alias (inside your profile):

Set-Alias git "git.cmd"
quadroid
  • 8,444
  • 6
  • 49
  • 82