gpt4 book ai didi

git - PowerShell 捕获 Git 输出

转载 作者:太空狗 更新时间:2023-10-29 14:28:57 24 4
gpt4 key购买 nike

问题

我正在通过 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 命令的输出,或者可以指出我做错了什么?

最佳答案

正如我所解释的here , 或 here , Git 命令输出在 stderr 上完成,而不是在 stdout 上完成。

因此,在您的情况下,您需要通过管道传输 stderr。
例如参见“Redirection of standard and error output appending to the same log-file”。

注意: with Git 2.16 (Q1 2018),可以先尝试设置

 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

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

关于git - PowerShell 捕获 Git 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47356782/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com