git - 特定git命令的颜色输出

标签 git

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

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

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

最佳答案

您可以使用-c option覆盖配置值。这适用于所有 git 命令。例如:

git -c color.ui=always status > status_file

将颜色代码输出到文件中。它看不到 pty,因此不会尝试分页。

关于git - 特定git命令的颜色输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23107594/

相关文章:

git - 列出 Git 在 a 和 b 之间的提交,仅考虑 b 的第一个 parent 和 a 的第一个 parent

Git - 多环境开发

git - 如何列出已推送到远程的分支?

node.js - 如何使用 Jenkinsfile 执行 Node.JS API 测试用例

git - 处理拒绝/拒绝的 pull 请求

git - 为什么我通过1970年的go-git提交内容?

git - 如何读取 Jenkins 中的 "Git Build Data"?

Git 记录到某个提交

git - 如何在 GitHub for Windows 中提交除一个文件以外的所有文件

git - 如何通过 http 启用对 git 存储库的匿名读取访问( pull )但经过身份验证的写入访问(推送)?