特定git命令的颜色输出 [英] Color output of specific git command

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

问题描述

我正在编写一个运行git命令并捕获输出的工具,并且希望对输出进行着色. Git注意到该工具不是终端,因此需要将color.ui设置为always.我真的不想在global/repo配置文件中设置它,因为它会与其他使用git的程序混淆. git书明确禁止这样做:

I'm writing a tool that runs git commands and captures the output, and I'd like to have the output colored. Git notices that the tool isn't a terminal, so the color.ui would need to be set to always. I don't really want to set this in the global/repo configuration file, as it would mess with other programs using git. The git book explicitly discourages that, too:

您很少需要color.ui =始终.在大多数情况下,如果要在重定向的输出中使用颜色代码,则可以将--color标志传递给Git命令,以强制其使用颜色代码. color.ui = true设置几乎总是您要使用的设置.

You’ll rarely want color.ui = always. In most scenarios, if you want color codes in your redirected output, you can instead pass a --color flag to the Git command to force it to use color codes. The color.ui = true setting is almost always what you’ll want to use.

遗憾的是,并不是所有的git命令都支持--color标志,最显着的是git statusgit pull.

Sadly, not all git commands support the --color flag, most notably git status and git pull.

那么,如何为一个git命令强制设置颜色?是否有我可以设置的配置变量,例如一个自定义的git配置文件?

So, how do I force colors for one git command? Is there a configuration variable I can set, e.g. for a custom git config file?

推荐答案

您可以使用 选项覆盖配置值.这适用于所有git命令.例如:

You can use the -c option to override config values. This works for all git commands. For example:

git -c color.ui=always status > status_file

将颜色代码输出到文件中.它没有显示任何内容,因此不会尝试分页.

outputs the color codes into the file. It does not see a pty, so it will not try to paginate.

这篇关于特定git命令的颜色输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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