如何漂亮地打印出 git-diff 输出?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12643615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 07:33:40  来源:igfitidea点击:

How do I prettily print out a git-diff output?

gitprintingdiffgit-diff

提问by Timo Josten

for my upcoming Bachelor thesis, I am required to print out the git-diff output for the appendix of the paper. I really like the output of git diff --color-words, but I have absolutely no idea how to bring this - colored and with line numbers included - to a piece of paper on my Mac. I know there are tools like SourceTree, but even with those I seem not able to make a pretty print out of the single file's diffs.

对于我即将发表的学士论文,我需要打印出论文附录的 git-diff 输出。我真的很喜欢 的输出git diff --color-words,但我完全不知道如何将它 - 彩色并包含行号 - 带到我的 Mac 上的一张纸上。我知道有像SourceTree这样的工具,但即使使用这些工具,我似乎也无法从单个文件的差异中进行漂亮的打印。

Well, I am pretty sure that I cannot be the only one with this problem, and I found some suggested ansi2html solutions, but they to not seem to work with my version of git (1.7.10.2). I also thought about simply printing the Github commit page (which is really pretty and kind of looks like what I need), but the browser will automatically remove all coloring and formatting when I am trying to print it.

好吧,我很确定我不会是唯一一个遇到这个问题的人,我找到了一些建议的 ansi2html 解决方案,但它们似乎不适用于我的 git (1.7.10.2) 版本。我还想过简单地打印 Github 提交页面(这真的很漂亮,看起来很像我需要的),但是当我尝试打印时,浏览器会自动删除所有颜色和格式。

Thanks for your help!

谢谢你的帮助!

采纳答案by onionjake

Use aha. You can install it in ubuntu with sudo apt-get install aha. Also see https://github.com/theZiz/aha.

使用aha. 你可以在 ubuntu 中安装它sudo apt-get install aha。另请参阅 https://github.com/theZiz/aha

$ git diff --color-words | aha > index.html
$ firefox index.html

Firefox should then be able to print it in color. Check out aha --helpfor some other cool options.

然后 Firefox 应该能够以彩色打印它。查看aha --help其他一些很酷的选项。

回答by ithil

I just tried an npm package hosted on Github: pretty-diff

我刚刚尝试了一个托管在 Github 上的 npm 包:pretty-diff

You install it via npm and run it as you would with git-diff, for example:

您可以通过 npm 安装它并像使用 一样运行它git-diff,例如:

pretty-diff HEAD^

A new browser window opens and you can save it as html. I've tried to open it again in a different browser and colors are still showing. You can also use your github account to create gists and share them.

将打开一个新的浏览器窗口,您可以将其另存为 html。我尝试在不同的浏览器中再次打开它,但颜色仍在显示。您还可以使用您的 github 帐户创建要点并共享它们。

回答by Jonathan Graham

Since you are using a Mac with the terminal application, it is as simple as cut and pasting from the terminal to another application.

由于您使用的是带有终端应用程序的 Mac,它就像从终端剪切并粘贴到另一个应用程序一样简单。