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

5

24

I am required to print out the git diff output for the appendix of a thesis paper. I really like the output of git diff --color-words, but I have absolutely no idea how to bring this formatting, with the colors 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 cannot seem to make a pretty print out of a single file's diffs.

I found some suggested ansi2html solutions, but they do 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.

Noncommittal answered 28/9, 2012 at 16:12 Comment(7)
diff the output into a file and print the file could be an option.Plumose
Yep, but then all the color is gone. :(Noncommittal
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.Plumose
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..Noncommittal
this was actually a good question. Hope you still get some good answers with easy solutions.Plumose
@TimoJosten it's nice for others to put the solution you found in an answer, and accept it. Thanks!Linnette
The kernel-demystified.com link above is dead 8(Courtesan
P
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.

Pomegranate answered 31/12, 2013 at 17:50 Comment(2)
commenting to add: for people who want the normal diff output, just use --color instead of --color-wordsGabler
Any solution for windows?Aleron
T
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.

Thyrsus answered 17/3, 2016 at 14:26 Comment(0)
F
1

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

Flynt answered 6/9, 2019 at 18:34 Comment(0)
T
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 can also set up an alias in your shell profile to avoid typing it out every time.

Also delta is awesome.

Offical Description: A syntax-highlighting pager for git, diff, and grep output

Even if you not set it as default pager in your configuration you can pipe the git diff output into it.

git diff origion/main | delta --side-by-side

is what I use daily

Tyro answered 7/5, 2022 at 19:4 Comment(0)
L
-1

If anybody wanted to print the diff output in a GitHub comment with line highlights:

git diff > changes.txt

This will make a file changes.txt with all the needed diffs, but when you paste it in the comment, it will not have highlights. You need to start the comment with:

```diff

Leave a line and then print the contents of changes.txt

Laboy answered 13/4, 2024 at 6:27 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Stocking

© 2022 - 2025 — McMap. All rights reserved.