Posh-git: Git Checkout from Powershell ISE displayes error message but works fine.

Created on 25 Jul 2013  Â·  8Comments  Â·  Source: dahlbyk/posh-git

When I checkout a branch from Powershell ISE I get an error message like this:

git : Switched to branch 'newBranch'
At line:1 char:1

  • git checkout newBranch
  • ~~~~~~

    • CategoryInfo : NotSpecified: (Switched to branch 'newBranch':String) [], RemoteException

    • FullyQualifiedErrorId : NativeCommandError

If I checkout from powershell console I don't get the message. The answer for this question http://stackoverflow.com/questions/2095088/error-when-calling-3rd-party-executable-from-powershell-when-using-an-ide explains how Powershell Console and Powershell ISE handle standard error differently but I don't understand why anything is getting sent to standard error when you checkout.

Any idea what's going on here?
Thanks,
Rob

Most helpful comment

I know this is an old one, but just in case anyone reads this after me: you can use -q option to stop progress reporting, like this: git clone -q <otherparams>. This will remove all the unnecessary info powershell would show.

All 8 comments

This is git being stupid, I'm afraid. Many git commands send output to stderr that, quite frankly, should be sent to stdout instead. Checkout is one such command. In the console (not ISE) try this:

PS> git checkout branch1
Switched to branch 'branch1'
PS> git checkout master
Switched to branch 'master'
PS> git checkout branch1 2>&1
git : Switched to branch 'branch1'
At line:1 char:1

  • git checkout branch1 2>&1
  • ~~~~

    • CategoryInfo : NotSpecified: (Switched to branch 'branch1':String) [], RemoteException

    • FullyQualifiedErrorId : NativeCommandError

I really don't like how PowerShell handles stderr, but TRWTF is git sending this information to stderr.

Ah that's annoying. Thanks for the info though. Any known workarounds other than just sending stderr to $null?

Not that I know of. Closing since this is not really something I think we can fix, but feel free to continue discussion.

Some unsatisfactory workarounds:

git checkout branch 2>&1 | % { $_.ToString() } # I've read this can get some lines out of order, but I don't know why.
(cmd /c git checkout branch 2>&1) # Sure to work, but less efficient and a pita. OK for scripts, I guess.

This is one really good argument for the PowerShell wrappers that are being discussed.

@wekempf thanks, I couldn't figure out how to get rid of all that extra "at line:1..." stuff. But yeah, the best solution for now may be to just get used to the error being there.

I know this is an old one, but just in case anyone reads this after me: you can use -q option to stop progress reporting, like this: git clone -q <otherparams>. This will remove all the unnecessary info powershell would show.

Another option besides the suggestion of @giggio is add the flag --porcelain on to the command, this formats the text that is meant to be used with scripting.

Here is a comparision of the two with git push (running in powershell with 0.7.1 version of the posh-git module)

C:\Source [master ↑14]> git push
git : Everything up-to-date
At line:1 char:1
+ git push
+ ~~~~~~~~
    + CategoryInfo          : NotSpecified: (Everything up-to-date:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

C:\Source [master ≡]> git push --porcelain
To https://hsah.visualstudio.com/BlueSkyNA/_git/RXWorks
=   refs/heads/master:refs/heads/master [up to date]
Done

However this does not work with checkout 😦

It looks like you can now redirect stderr to stdout throughout your powershell script by simply setting an environment variable:

$env:GIT_REDIRECT_STDERR = '2>&1'
Was this page helpful?
0 / 5 - 0 ratings