Problem
I am running some git commands through PowerShell but I am having issues capturing the output.
Many of the ways I have looked into have failed to capture any output at all until I tried using Out-File
. Unfortunately, despite how well this has been working for me, I have found that something is going wrong with the git push
command that is preventing me from capturing the output...
My code looks 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 but the output of the git push
. So in troubleshooting this, I removed the | Out-File
from the line and noticed that even the output window was not showing the output of the git push
anymore. It was only when I completely removed the Out-File
from all of the git commands that I was able to get the output to display as expected again. Unfortunately, this put me back at square one.
Question
Does anyone know how to best capture the output of git commands using PowerShell, or can point out what I am doing wrong?
As I explained here, or here, Git command output are done on stderr, not stdout.
So in your case, you need to pipe stderr.
See "Redirection of standard and error output appending to the same log-file" for instance.
Note: with Git 2.16 (so not yet released), you can try and set first
Then stderr should be redirected to stdout.
In "PowerShell - Capturing STDERR Output for Git Commands", the OP Brandon points out to the
--porcelain
option of (for instance) git push:It allows to convert the outputs into a machine readable format and pipe them to the output.