Git diff输出到文件保留着色


Answers:


145

尝试:

git diff --color > foo.txt

然后再发布:

cat foo.txt

要么:

less -R foo.txt

2
@RoR,一个人的“乱码”是另一个人(以及终端的)的颜色代码。;-)我试过了,它起作用了;如果cat从命令行下载文件,则保留颜色。(您还希望颜色如何保留?)
mpontillo 2012年

1
嗯,这就是它的作用。添加bash着色代码。如果在控制台中放置文件,它将显示颜色。
ralphtheninja 2012年

1
@RoR您必须在其中放置一些内容,以将bash颜色代码转换为rtf格式。
ralphtheninja 2012年

3
Notepad ++可以突出显示差异文件。(* .diff扩展名会自动突出显示,否则请更改Language --> D --> Diff。)如果您不喜欢默认颜色,请在中进行更改Settings --> Style Configurator --> Diff。要复制带有颜色的粘贴,您可以使用NppExport允许导出/复制到RTF或HTML 的插件(例如)。
Nikita G.

3
git diff AB> foo.diff(许多编辑器将打开带有颜色的文件,因为该文件是.diff文件)
zeusstl

34

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

git diff > 20150203_someChanges.diff

谢谢@Monsingor


我认为您忘了中间的'>',这行不通
GoGoris

20

Sublime Text 2中打开输出diff文件。它显示差异颜色。


1
如果diff文件的扩展名不同于* .diff,则可能需要将语法更改为diff以启用正确的突出显示。(通过视图->语法->差异)。
Nikita G.

diff.txt即使在右下角自动检测到“ Diff”,它也不会突出显示。
Nakilon

10

扩展@Gabe的答案。

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

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

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

ansi2html代码在此处:http ://www.pixelbeat.org/scripts/ansi2html.sh


不起作用,我得到:gawk:cmd。行:25:(FILENAME =-FNR = 1)致命:尝试在标量上下文中使用数组“ a(来自跨度)”
Tim

2
获取Homebrew并运行brew install gawk。您还需要brew install gnu-sed
Geoffrey Booth

正是我想要的,太棒了!
基思(Keith)

ansi2html python库为我完成了github.com/ralphbean/ansi2html。pip可安装,并且与该答案中的bash脚本完全一样。不要忘记将“ ./ansi2html.sh”替换为“ ansi2html”。
Nagasaki45



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

SublimeText2可以轻松读取“ * .txt”文件中提取的差异,而无需进行设置(通过查看->语法->差异)。


git remote rm remotes/b/master重置分支回到它的原始状态。
阿披耶特(Abhijeet)

1

允许从浏览器查看任何彩色的终端文本... 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

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.