23

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.

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.

Thanks for your help!

7
  • diff the output into a file and print the file could be an option.
    – three
    Sep 28, 2012 at 16:15
  • 1
    Yep, but then all the color is gone. :( Sep 28, 2012 at 16:15
  • use quickdiff.com to diff and copy the output into TextEdit or any other html aware editor to print it. Sounds stupid but thats a simple way of doing it.
    – three
    Sep 28, 2012 at 16:18
  • Well, the point is, I do not have a "before" and "after" / "left" and "right" file, but only a ready diff file. I also cannot just checkout the old and then the new version of the files, because we are talking about hundreds of files and thousands of lines changed.. Sep 28, 2012 at 16:20
  • this was actually a good question. Hope you still get some good answers with easy solutions.
    – three
    Sep 28, 2012 at 17:32

4 Answers 4

19

Use aha. You can install it in ubuntu with sudo apt-get install aha. Also see 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 --help for some other cool options.

2
  • commenting to add: for people who want the normal diff output, just use --color instead of --color-words
    – fifaltra
    May 4, 2020 at 9:44
  • Any solution for windows?
    – Rishav
    Jun 22, 2020 at 18:15
11

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

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

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.

0
2

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

1

What i found pretty useful is to just pipe it into bat, a modern cat. If you want only a specific diff the command can be used as usual.

git diff | bat

You cann also set up a alias in your shell profile to avoid typing it out every time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.