170

Is it possible to do git diff and save the output to a file with the coloring somehow?

I know how to do git diff > filename.rtf - which saves to a file, but I'd like to preserve the coloring.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
RoR
  • 14,904
  • 22
  • 67
  • 92

9 Answers9

190

Try:

git diff --color > foo.txt

Then later issue:

cat foo.txt

Or:

less -R foo.txt
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
ralphtheninja
  • 117,827
  • 20
  • 105
  • 119
  • 3
    @RoR, one person's "gibberish" is another person's (well, terminal's) color codes. ;-) I tried it and it works; the coloring is preserved if I `cat` the file from the command-line. (How else would you expect the colors to be preserved?) – mpontillo Mar 14 '12 at 17:20
  • 1
    Well that's what it does. Adds bash coloring codes. If you cat the file in the console it shows the colors. – ralphtheninja Mar 14 '12 at 17:20
  • 1
    @RoR You'd have to put something in the middle there to convert the bash color codes to rtf format. – ralphtheninja Mar 14 '12 at 17:23
  • 5
    Notepad++ can highlight diff files. (*.diff extensions are automatically highlighted, otherwise change `Language --> D --> Diff`.) If you don't like default colors, change them in `Settings --> Style Configurator --> Diff`. To copy paste with colors you can use a plugin (e.g. `NppExport`) that allows exporting/copying into RTF or HTML. – Nikita R. Oct 22 '15 at 22:31
  • 5
    git diff A B > foo.diff (Many editors will open the file with the colors because the file is a .diff file) – zeusstl Mar 18 '16 at 13:12
  • Is there a way to export the git diff to wiki markup (with colors) – Tarun Sapra Dec 19 '17 at 13:53
64

Save the file with a .diff extension and open it in Notepad++ or Vim or SublimeText.

git diff > 20150203_someChanges.diff

Thanks @Monsingor

ironhyde
  • 816
  • 7
  • 11
23

Open the output diff file in Sublime Text 2. It shows the diff colors.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Julien
  • 239
  • 2
  • 2
  • 1
    You might need to change syntax to diff to enable proper highlighting in case the diff file has extension different from *.diff. (via View -> Syntax -> Diff). – Nikita R. Oct 22 '15 at 22:27
  • It doesn't highlight `diff.txt` for me, even with automatically detected "Diff" in the right bottom corner. – Nakilon Sep 20 '16 at 20:31
18

To expand on @Gabe's answer.

You can pipe the output to an ansi to html converter bash script and direct that output to an html file:

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

of course html can be viewed by any browser so output can be read in Windows etc.

ansi2html code is here: http://www.pixelbeat.org/scripts/ansi2html.sh

Nakilon
  • 33,683
  • 14
  • 104
  • 137
sk8asd123
  • 1,575
  • 15
  • 14
  • doesn't work I get: gawk: cmd. line:25: (FILENAME=- FNR=1) fatal: attempt to use array `a (from span)' in a scalar context – Tim May 19 '15 at 01:59
  • 2
    Get Homebrew and run `brew install gawk`. You'll also need `brew install gnu-sed`. – Geoffrey Booth Oct 30 '15 at 19:53
  • exactly what i want, awesome ! – Keith Jul 02 '19 at 05:15
  • 1
    The ansi2html python library did it for me https://github.com/ralphbean/ansi2html. pip installable and works exactly as the bash script in this answer. Don't forget to replace "./ansi2html.sh" with "ansi2html". – Nagasaki45 Feb 04 '20 at 00:13
9

Vim colors files containing git diff's beautifully.

git diff

Rose Perrone
  • 58,751
  • 52
  • 202
  • 239
5

I found an answer here: Color output of specific git command.

You can pass -c color.ui=always to any git command and it will keep coloring on redirection. For example: git -c color.ui=always status > file

Community
  • 1
  • 1
amaslenn
  • 702
  • 9
  • 16
1
git remote add -f b path/to/repo_b.git
git remote update
git diff master remotes/b/master > foo.txt

Differences extracted in '*.txt' files are easily read by SublimeText2 without the need to set (via View -> Syntax -> Diff).

Abhijeet
  • 8,114
  • 5
  • 68
  • 74
1

to allow any colorized terminal text ... git diff or any other ... to be viewable from a browser

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

install aha using above then issue

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

firefox  ~/cool_colorized.html
Scott Stensland
  • 24,813
  • 12
  • 87
  • 96
0

You could upload to GitHub and provide a link to the relevant commit.

Choylton B. Higginbottom
  • 2,156
  • 3
  • 22
  • 32