我正在通过PowerShell运行一些git命令,但是我在捕获输出时遇到了问题。
在我尝试使用Out-File
之前,我所研究的许多方法都未能捕获任何输出。不幸的是,尽管这对我来说有多好,但我发现git push
命令出现了问题,导致我无法捕获输出......
我的代码如下:
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
当我查看日志文件时,它会显示除git push
的输出之外的所有内容。因此,在对此进行故障排除时,我从行中删除了| Out-File
,并注意到即使输出窗口也没有显示git push
的输出。只有当我从所有git命令中完全删除Out-File
时,才能使输出再次按预期显示。不幸的是,这让我回到了原点。
有谁知道如何使用PowerShell最好地捕获git命令的输出,或者可以指出我做错了什么?
答案 0 :(得分:6)
正如我解释here或here,Git命令输出是在stderr上完成的,而不是stdout。
所以在你的情况下,你需要管道stderr 见" Redirection of standard and error output appending to the same log-file"例如。
注意: with Git 2.16 (所以尚未发布),您可以尝试先设置
set GIT_REDIRECT_STDERR=2>&1
然后应该将stderr重定向到stdout。
在" PowerShell - Capturing STDERR Output for Git Commands"中,OP Brandon指向--porcelain
option of (for instance) git push:
& git status --porcelain >> $logFilePath
& git push --porcelain >> $logFilePath
它允许将输出转换为机器可读格式并将它们输出到输出。