217

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.

2

10 Answers 10

250

Try:

git diff --color > foo.txt 

Then later issue:

cat foo.txt 

Or:

less -R foo.txt 

https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration:

Git automatically colors most of its output, but there’s a master switch if you don’t like this behavior. To turn off all Git’s colored terminal output, do this:

$ git config --global color.ui false

The default setting is auto, which colors output when it’s going straight to a terminal, but omits the color-control codes when the output is redirected to a pipe or a file.

You can also set it to always to ignore the difference between terminals and pipes. You’ll rarely want this; in most scenarios, if you want color codes in your redirected output, you can instead pass a --color flag to the Git command to force it to use color codes. The default setting is almost always what you’ll want.

Sign up to request clarification or add additional context in comments.

7 Comments

@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?)
Well that's what it does. Adds bash coloring codes. If you cat the file in the console it shows the colors.
@RoR You'd have to put something in the middle there to convert the bash color codes to rtf format.
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.
git diff A B > foo.diff (Many editors will open the file with the colors because the file is a .diff file)
|
90

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

git diff > 20150203_someChanges.diff 

Thanks @Monsingor

1 Comment

the best answer I found so far it works also with VS Code
23

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

2 Comments

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).
It doesn't highlight diff.txt for me, even with automatically detected "Diff" in the right bottom corner.
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

5 Comments

doesn't work I get: gawk: cmd. line:25: (FILENAME=- FNR=1) fatal: attempt to use array `a (from span)' in a scalar context
Get Homebrew and run brew install gawk. You'll also need brew install gnu-sed.
exactly what i want, awesome !
The ansi2html python library did it for me 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".
In Debian-like, install kbtin and type git diff --word-diff --color commit1 commit2 | ansi2html > changes.html.
8

Vim colors files containing git diff's beautifully.

git diff

Comments

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

Comments

3

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 

Comments

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 

Comments

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).

1 Comment

git remote rm remotes/b/master to reset branch back to it's original state.
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.