PowerShell 捕获 Git 输出 [英] PowerShell Capture Git Output

查看:24
本文介绍了PowerShell 捕获 Git 输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过 PowerShell 运行一些 git 命令,但在捕获输出时遇到问题.

I am running some git commands through PowerShell but I am having issues capturing the output.

在我尝试使用 Out-File 之前,我研究的许多方法都无法捕获任何输出.不幸的是,尽管这对我来说效果很好,但我发现 git push 命令出了点问题,导致我无法捕获输出...

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...

我的代码如下:

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 时,我才能让输出再次按预期显示.不幸的是,这让我回到了原点.

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.

有谁知道如何最好地使用 PowerShell 捕获 git 命令的输出,或者可以指出我做错了什么?

Does anyone know how to best capture the output of git commands using PowerShell, or can point out what I am doing wrong?

推荐答案

正如我在此处这里,Git 命令输出是在 stderr 上完成的,而不是在 stdout 上.

As I explained here, or here, Git command output are done on stderr, not stdout.

所以在你的情况下,你需要管道 stderr.
例如,请参见重定向标准和错误输出附加到同一日志文件".

So in your case, you need to pipe stderr.
See "Redirection of standard and error output appending to the same log-file" for instance.

注意:使用 Git 2.16(2018 年第一季度),您可以先尝试设置

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

 set GIT_REDIRECT_STDERR=2>&1

然后应该将 stderr 重定向到 stdout.

Then stderr should be redirected to stdout.

PowerShell - 捕获 Git 命令的 STDERR 输出"中,OP Brandon 指出 --porcelain 选项(例如)git push:

In "PowerShell - Capturing STDERR Output for Git Commands", the OP Brandon points out to the --porcelain option of (for instance) git push:

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

它允许将输出转换为机器可读格式并将它们通过管道传输到输出.

It allows to convert the outputs into a machine readable format and pipe them to the output.

这篇关于PowerShell 捕获 Git 输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆