简体   繁体   English

Powershell 未捕获彩色 Git output

[英]Powershell not capturing colorized Git output

I'm executing the following command in Powershell.我在 Powershell 中执行以下命令。

 git log --branches --not --remotes --oneline

The goal is to get all non-pushed commits on all branches.目标是在所有分支上获取所有非推送提交。 Git displays the name of the branch in the output (in a different color). Git 显示 output 中的分支名称(不同颜色)。 When I try to capture the command with当我尝试捕获命令时

 $foo = git log --branches --not --remotes --oneline

the variable $foo is missing the colored output.变量$foo缺少彩色 output。 So it's not the same as what gets displayed on the screen.所以它与屏幕上显示的内容不同。

How can I capture the same output that would be displayed?如何捕获将显示的相同 output?

Update Just to clarify, I don't care about the output being in color.更新只是为了澄清,我不在乎 output 是彩色的。 My point was only that the colored output is not being captured at all, in color or not我的观点只是彩色 output 根本没有被捕获,无论是否有颜色

This is not a PowerShell issue but actually with Git.这不是 PowerShell 问题,而是 Git 问题。 Git changes the output depending on how the output is processed. Git 根据 output 的处理方式更改 output。 In GNU/Linux, for example:在 GNU/Linux 中,例如:

$ git log --branches --not --remotes --oneline
314129a (HEAD -> master) Generate Bootstrap rules with mode classes.
...

If I pipe it through cat for example then the output changes to:例如,如果我通过cat pipe 它,则 output 更改为:

$ git log --branches --not --remotes --oneline | cat
314129a Generate Bootstrap rules with mode classes.
...

It wasn't just the colour in your example as the parantheses were removed as well.这不仅仅是您示例中的颜色,因为括号也被删除了。

What you are seeing in PowerShell is the same behaviour:您在 PowerShell 中看到的是相同的行为:

PS> git log --branches --not --remotes --oneline
d331837 (HEAD -> master) Fixed issue with comparisons.

And then piped through something:然后通过管道传递一些东西:

PS> git log --branches --not --remotes --oneline | Out-String
d331837 Fixed issue with comparisons.

And so to fix the problem you need to tell Git to generate output in a specific way, eg因此,要解决问题,您需要告诉 Git 以特定方式生成 output,例如

$foo = git log --branches --not --remotes --oneline --decorate=short

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM