197

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,738
  • 21
  • 105
  • 131
RoR
  • 15,934
  • 22
  • 71
  • 92

10 Answers10

222

Try:

git diff --color > foo.txt

Then later issue:

cat foo.txt

Or:

less -R foo.txt
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
rtn
  • 127,556
  • 20
  • 111
  • 121
  • 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. – rtn 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. – rtn Mar 14 '12 at 17:23
  • 6
    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
76

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

git diff > 20150203_someChanges.diff

Thanks @Monsingor

ironhyde
  • 936
  • 7
  • 12
23

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Julien
  • 247
  • 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
  • 1
    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
20

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
  • 34,866
  • 14
  • 107
  • 142
sk8asd123
  • 1,665
  • 16
  • 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
  • 2
    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
  • In Debian-like, install [kbtin](https://packages.debian.org/search?keywords=kbtin) and type `git diff --word-diff --color commit1 commit2 | ansi2html > changes.html`. – Olivier Cailloux Oct 26 '22 at 11:00
8

Vim colors files containing git diff's beautifully.

git diff

Rose Perrone
  • 61,572
  • 58
  • 208
  • 243
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
  • 797
  • 9
  • 16
2

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
  • 26,870
  • 12
  • 93
  • 104
2

As an alternative to file redirection, you also have the git diff --output option

git diff --color --output=aFile
cat aFile
# you would still see the colors

However, make sure to not use the combined diff format (for diff on merge commits), like git diff -c or git diff --cc

With Git 2.38 (Q3 2022), certain diff options (including --output) are currently ignored when combined-diff is shown; mark them as incompatible with the feature.

See commit cfb19ae, commit e3d1be4 (18 Jun 2022) by René Scharfe (rscharfe).
(Merged by Junio C Hamano -- gitster -- in commit a2d1f00, 11 Jul 2022)

combine-diff: abort if --output is given

Reported-by: Ævar Arnfjörð Bjarmason
Signed-off-by: René Scharfe

The code for combined diffs currently only writes to stdout.
Abort and report that fact instead of silently ignoring the --output option.
The (empty) output file has already been created at that point, though.

The error message would therefore be:

combined diff and '--output' cannot be used together
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
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,561
  • 5
  • 70
  • 76
0

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

Choylton B. Higginbottom
  • 2,236
  • 3
  • 24
  • 34