PowerShell Capture Git Exit

Problem

I run several git commands through PowerShell, but I'm having problems grabbing output.

Many of the ways I was looking for were unable to capture any output at all until I tried using Out-File . Unfortunately, despite how well this works for me, I found that something was wrong with the git push command, which was stopping me from grabbing the output ...

My code is as follows:

 git add . | Out-File $logFilePath -Append git status . | Out-File $logFilePath -Append git commit -m "Some Relevant update message" | Out-File $logFilePath -Append git push origin $branchname | Out-File $logFilePath -Append 

When I look at the log files, it shows everything except the output of git push . Therefore, when troubleshooting, I deleted | Out-File | Out-File from the line and noticed that even the output window no longer displays git push output. Only when I completely removed Out-File from all git commands so that I can display the output again as expected. Unfortunately, this brought me back to the square.

Question

Does anyone know what is the best way to capture the output of git commands using PowerShell or indicate what I am doing wrong?

+2
source share
1 answer

As I explained here or here , the output of the Git command is executed on stderr, not stdout.

So in your case you need a stderr pipe.
See, for example, " Redirecting standard output and error output when added to the same log file ."

Note: with Git 2.16 (Q1 2018), you can try and install first

  set GIT_REDIRECT_STDERR=2>&1 

Then stderr should be redirected to standard output.

In " PowerShell - Capturing STDERR Output for Git Commands ", OP Brandon points to --porcelain (for example) git push :

 & git status --porcelain >> $logFilePath & git push --porcelain >> $logFilePath 

This allows you to convert the output into a machine-readable format and transfer it to the output.

+6
source

Source: https://habr.com/ru/post/1274844/


All Articles