繁体   English   中英

Git diff 输出到文件保留颜色

[英]Git diff output to file preserve coloring

是否可以执行git diff并将输出保存到带有颜色的文件中?

我知道如何做git diff > filename.rtf - 它保存到文件中,但我想保留颜色。

尝试:

git diff --color > foo.txt

然后后面的问题:

cat foo.txt

或者:

less -R foo.txt

使用 .diff 扩展名保存文件并在 Notepad++ 或 Vim 或 SublimeText 中打开它。

git diff > 20150203_someChanges.diff

谢谢@Monsingor

Sublime Text 2中打开输出差异文件。 它显示了不同的颜色。

扩展@Gabe的答案。

您可以将输出通过管道传输到 ansi 到 html 转换器 bash 脚本,并将该输出定向到 html 文件:

git diff --color|./ansi2html.sh > changes.html

当然,任何浏览器都可以查看 html,因此可以在 Windows 等中读取输出。

ansi2html 代码在这里: http ://www.pixelbeat.org/scripts/ansi2html.sh

包含 git diff 的 Vim 颜色文件精美。

混帐差异

我在这里找到了答案: Color output of specific git command

您可以将-c color.ui=always传递给任何git命令,它会在重定向时保持着色。 例如: git -c color.ui=always status > file

git remote add -f b path/to/repo_b.git
git remote update
git diff master remotes/b/master > foo.txt

SublimeText2 可以轻松读取在 '*.txt' 文件中提取的差异,而无需设置(通过 View -> Syntax -> Diff)。

允许从浏览器查看任何彩色终端文本... git diff 或任何其他...

sudo apt-get install aha  #  https://github.com/theZiz/aha

使用上面安装aha然后发出

git diff --color mysourcefile  | aha > ~/cool_colorized.html

firefox  ~/cool_colorized.html

您可以上传到 GitHub 并提供相关提交的链接。

作为文件重定向的替代方案,您还可以使用git diff --output选项

git diff --color --output=aFile
cat aFile
# you would still see the colors

但是,请确保不要使用组合 diff 格式( for diff on merge commits ),例如git diff -cgit diff --cc

在 Git 2.38(2022 年第三季度)中,当显示 combine-diff 时,某些 diff 选项(包括--output )当前会被忽略; 将它们标记为与该功能不兼容。

请参阅René Scharfe ( rscharfe )提交 cfb19ae提交 e3d1be4 (2022 年 6 月 18 日)。
(由Junio C Hamano -- gitster --提交 a2d1f00中合并,2022 年 7 月 11 日)

combine-diff : 如果给出 --output 则中止

报告人:Ævar Arnfjörð Bjarmason
签字人:René Scharfe

组合差异的代码目前仅写入标准输出。
中止并报告该事实,而不是默默地忽略--output选项。
不过,此时已经创建了(空)输出文件。

因此,错误消息将是:

combined diff and '--output' cannot be used together

暂无
暂无

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

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