Highlight changed lines and changed bytes in each changed line

24,234

Solution 1

The diff-highlight Perl contrib script produces output so similar to that of the Trac screenshots that it is likely that Trac is using it:

enter image description here

Install with:

wget https://raw.githubusercontent.com/git/git/fd99e2bda0ca6a361ef03c04d6d7fdc7a9c40b78/contrib/diff-highlight/diff-highlight && chmod +x diff-highlight

Move the file diff-highlight to the ~/bin/ directory (or wherever your $PATH is), and then add the following to your ~/.gitconfig:

[pager]
        diff = diff-highlight | less
        log = diff-highlight | less
        show = diff-highlight | less

Single copy paste install suggested by @cirosantilli:

cd ~/bin
curl -O https://raw.githubusercontent.com/git/git/fd99e2bda0ca6a361ef03c04d6d7fdc7a9c40b78/contrib/diff-highlight/diff-highlight
chmod +x diff-highlight
git config --global pager.log 'diff-highlight | less'
git config --global pager.show 'diff-highlight | less'
git config --global pager.diff 'diff-highlight | less'
git config --global interactive.diffFilter diff-highlight

Solution 2

While using git diff or git log and possibly others, use option --word-diff=color (there are also other modes for word diffs BTW)

Solution 3

diff-so-fancy is a diff-highlighter designed for human eyeballs.

It removes the leading +/- which are annoying for cut/paste and makes clear sections between files.

Coloured git (left) vs diff-so-fancy (right - note the character-level highlights):

diff-so-fancy output

If you want thediff-so-fancy (right side) output but not constrained to files in a git repository, add the following function to your .bashrc to use it on any files:

dsf() { git diff --no-index --color "$@" | diff-so-fancy; }

Eg:

dsf original changed-file

Character level highlighting and standard diff format

If you don't like the non-standard formatting of diff-so-fancy, but still want character-level git highlighting, use diff-highlight which will take git's output and produce the really pretty standard diff-format output:

diff-highlight screenshot

To use it by default from git, add to your .gitconfig:

[color "diff-highlight"]
  oldNormal = red bold
  oldHighlight = red bold 52
  newNormal = green bold
  newHighlight = green bold 22

[pager]
  diff = diff-highlight | less -FRXsu --tabs=4

The [pager] section tells git to pipe its already colourised output to diff-highlight which colourises at the character level, and then pages the output in less (if required), rather than just using the default less.

Solution 4

A utility for byte-based diffs has been distributed with official Git since v1.7.81. You just have to locate where it is installed on your machine and enable it.

Find where Git is installed

  • MacOS with Git installed via Homebrew: It's /usr/local/opt/git (later versions: /opt/homebrew/Cellar/git/VERSION)
  • Windows with Git for Windows: Run cd / && pwd -W to find the install directory.
  • Linux: Nerd. If you don't already know where Git is installed, then ll $(which git) or locate git should help.

Link diff-highlight to your bin directory so that your PATH can find it

GIT_HOME='/usr/local/opt/git/'  # Use the value from the first step.
ln -s "${GIT_HOME}/share/git-core/contrib/diff-highlight/diff-highlight" \
      '/usr/local/bin/diff-highlight'

Enable it in your Git config

git config --global interactive.diffFilter diff-highlight # Use on interactive prompts
git config --global pager.diff "diff-highlight | less"    # Use on git diff
git config --global pager.log  "diff-highlight | less"    # Use on git log
git config --global pager.show "diff-highlight | less"    # Use on git show

1 Here is the v1.7.8 version, but lots of changes have been made since then.

Solution 5

The behaviour you want is now available in git itself (as was pointed out in a comment by naught101). To enable it you need to set your pager to

perl /usr/share/doc/git/contrib/diff-highlight/diff-highlight | less

where /usr/share/doc/git/contrib/diff-highlight/diff-highlight is the location of the highlighter script on Ubuntu 13.10 (I have no idea why it's in a doc folder). If it isn't there on your system try using locate diff-highlight to find it. Note that the highlighting script is not executable (at least on my machine), hence the requirement for perl.

To always use the highlighter for the various diff-like commands just add the following to your ~/.gitconfig file:

[pager]
    log = perl /usr/share/doc/git/contrib/diff-highlight/diff-highlight | less
    show = perl /usr/share/doc/git/contrib/diff-highlight/diff-highlight | less
    diff = perl /usr/share/doc/git/contrib/diff-highlight/diff-highlight | less

I added this as a new answer naught101's comment is buried and because the set up is not quite as trivial as it should be and at least on the version of Ubuntu that I have the instructions in the README don't work.

Share:
24,234

Related videos on Youtube

Nikolay Frantsev
Author by

Nikolay Frantsev

Technical Administrator of Mozilla Russia, Web/Software Developer, VIMer, Linux & Android User

Updated on July 08, 2022

Comments

  • Nikolay Frantsev
    Nikolay Frantsev almost 2 years

    Open Source project Trac has an excellent diff highlighter — it highlights changed lines and changed bytes in each changed line! See here or here for examples.

    Is there way to use the same color highlight (i.e. changed lines and changed bytes too) in bash terminal, git, or vim for diff output (patch-file)?

    • shellter
      shellter about 13 years
      What is it that you want to highlight? Do you want a diff tool that higlights the byte changes? (that would be very helpful). You say vim, to my recollection vim does a lot of color manipulation already when you are using programming language templates (and other). How would you change that? There are quite a few techniques available to change color on a terminal window that is defined VT100 (and there are dozens of other definitions that will also support color escape sequences). More specifics please. Or read en.wikipedia.org/wiki/VT100 and related links. Maybe that can help.
    • nhed
      nhed about 13 years
      I know you are only interested in open source tools, and only in terminal. But just as a reference point you may want to look at slickedit's diffzilla. of the few diff tools I used it has always seem to best represent character differences (though it definitly had issues when the diffs where complex (combination of formatting and code changes, which is always a bad idea)
    • Adam Monsen
      Adam Monsen over 11 years
    • VonC
      VonC over 9 years
      Note: GitHub now offers such a diff tool in its Web GUI: stackoverflow.com/a/25723584/6309
    • Zorglub29
      Zorglub29 about 4 years
      I have posted 'yet another' pure git, diff-highlight based solution with tutorials to easily 1) find the relevant diff-highlight file, 2) make it executable 3) set necessary params in .gitconfig. Please have a look. Instructions are for Ubuntu 18.04 but should work broadly on linux systems.
    • caw
      caw about 3 years
  • Nikolay Frantsev
    Nikolay Frantsev about 13 years
    I do not want to compare files, I want to highlight diff (patch) file.
  • Admin
    Admin about 13 years
    unfortunately, it does not highlight changed bytes on diff output (set filetype=diff)
  • PDug
    PDug about 13 years
    I think I understand your question now - You want to syntax highlight the textual output of the diff command so that it highlights any changes made inside of a line. Editing this text in Vim highlights line differences, but not the changes made within a line.
  • PDug
    PDug about 13 years
    Could you use Vim's :patchfile command to load the original file and then compare it to the patched version?
  • Admin
    Admin about 13 years
    unfortunately no, I want to use recursive diff output for multiple files
  • ZyX
    ZyX about 13 years
    @Nikolay Frantsev If you don't care about perfomance, you can install my format.vim plugin and do vimdiff file.old file.new -c 'FormatCommand diffformat' -c 'w! file.diff.html' -c 'qa!'.
  • ZyX
    ZyX about 13 years
    It will do a diff in a batch mode (prepend screen -D -m or append &>/dev/null (/dev/null variant sometimes produces strange bugs) if you don't want to see the terminal flashing) and quit vim after formatting is done, but it is pure vimscript and even with my optimizations it is very slow for large files.
  • Nikolay Frantsev
    Nikolay Frantsev about 13 years
    --word-diff=color is really better (especially with git config color.diff.old "red reverse" and git config color.diff.new "green reverse"), but it is not what I want :(
  • anydot
    anydot about 13 years
    So only thing you are missing is marking in colour/somehow both changed lines and bytes on same time?
  • Nikolay Frantsev
    Nikolay Frantsev about 13 years
    I want to highlight changed lines and changed bytes in each changed line, like in Trac. Not just changed bytes, it is not the same.
  • Admin
    Admin about 13 years
    sorry, I do not want to use emacs, only bash, git or vim
  • Finbar Crago
    Finbar Crago about 13 years
    That's understandable. The only other thing i can think of is to use colordiff with the stdout from patch: colordiff -u <(patch original_file -i patch_file -o -) <(cat original_file) but this is only going to highlight changed lines not bites...
  • Finbar Crago
    Finbar Crago about 13 years
    I gave your problem a bit more thought and have appended a second solution which requires only dwdiff.
  • Admin
    Admin about 13 years
    please read carefully my question, I do not want to compare files
  • Finbar Crago
    Finbar Crago about 13 years
    sorry for the confusion, so your just after a way to highlight the changed bytes on the changed lines of a diff file? if so try dwdiff -c --diff-input diff_file
  • Admin
    Admin about 13 years
    I tried it before writing previous comment. unfortunately it does not highlight changed lines, only changed bytes :(
  • naught101
    naught101 about 11 years
    No, this only shows the difference in words. What the OP (and I) want is a normal line-by-line diff, with the word differences highlighted (so, say different lines are coloured text, and the word-differences within those lines are normal coloured text, with coloured highlighting or something). See the example links now in the question.
  • naught101
    naught101 about 11 years
    This. This is excellent. Thank you. It seems to be a little bit conservative in some places though, missing some lines that obviously have a majority of text in common. Do you have a bug tracker for it?
  • naught101
    naught101 about 11 years
    Ah, this is part of core git now: github.com/git/git/tree/master/contrib/diff-highlight
  • amized
    amized almost 11 years
    pastebin.com/1JrhYHRt Actually I use vimdiff as difftool and vimdiff with molokai colorscheme to get a nice highlighting as you describe in your question. 1- git config --global diff.tool vimdiff 2- in vim ":colo molokai" * Molokai @ github.com/tomasr/molokai * Possible auto colorscheme with ~/.vimrc: if &diff set background=dark colorscheme molokai endif
  • naught101
    naught101 over 10 years
    You can also use this with git add --patch: stackoverflow.com/questions/10873882/…
  • Ciro Santilli OurBigBook.com
    Ciro Santilli OurBigBook.com about 10 years
    @s1n4: please add your explanation here also. Its the best answer.
  • Ciro Santilli OurBigBook.com
    Ciro Santilli OurBigBook.com almost 10 years
    The advantage of diff-highlight is that it works well for both word diffs and line diffs.
  • dshepherd
    dshepherd over 9 years
    I've just noticed that this doesn't enable the highlighting for the diffs within git add -p (interactive mode). I don't know how that can be fixed though, simply adding add to the list causes it to hang.
  • Chris
    Chris about 9 years
    Github's raw urls appear to have changed, so the curl command above needs to be updated to point to the current url or use the -L tag to follow the redirect header.
  • Thomas
    Thomas almost 8 years
    This should work now in git 2.9.0: git config interactive.diffFilter diff-highlight
  • caesarsol
    caesarsol about 7 years
    This is very interesting, could you explain a bit about these gitconfig options?
  • Tom Hale
    Tom Hale about 7 years
    Updated, also adding function dsf().
  • Chris Midgley
    Chris Midgley almost 7 years
    It's now been turned into a module, and I think the easiest version to download from is the one immediately before that change at raw.githubusercontent.com/git/git/…
  • Cory Klein
    Cory Klein over 5 years
    ^ This! Unfortunately, diff-highlight wasn't on my path so I had to locate it first. Details in my answer below.
  • Cory Klein
    Cory Klein over 5 years
    Not only is this part of core git, it's distributed with git and probably already on your system. I added details on how to enable it in my answer below. ↓
  • Cory Klein
    Cory Klein over 5 years
    If you enable diff-highlight it automatically highlights byte differences in all git patch diffs. Details in my answer below. ↓
  • Michael
    Michael over 5 years
    Here's the direct link to @CoryKlein's answer.
  • Ciro Santilli OurBigBook.com
    Ciro Santilli OurBigBook.com about 5 years
    It would be good to specify in which version it started to be distributed with git. Also l'm guessing that distros will put it in PATH by default, so the symlink step won't be needed? And which git requires it to be in the PATH in the first place so it won't work if it's not :-)
  • Cory Klein
    Cory Klein about 5 years
    It would be good! Feel free to add that information. And although Git does bundle diff-highlight, it does not actually install it, so the symlink step is indeed necessary (at least on macOS). If you find that it isn't necessary for your platform, again feel free to update the answer. Meanwhile, which git usually does work, because Git does install the git binary somewhere on the path.
  • josch
    josch about 5 years
    This misses the diffs you see via git add -p. Please also add: git config --global interactive.diffFilter diff-highlight
  • tobiasBora
    tobiasBora about 4 years
    Note that in debian unstable I needed to "compile" this file, because I just had a .perl. The compilation is trivial: just run sudo make in the diff-highlight directory.
  • young_souvlaki
    young_souvlaki over 2 years
    Most distros have diff-so-fancy in their repo, which uses diff-highlight.
  • young_souvlaki
    young_souvlaki over 2 years
    Most distros have this package in their repo: github.com/so-fancy/diff-so-fancy#install.
  • ricab
    ricab over 2 years
    One may need to run make in /usr/share/doc/git/contrib/diff-highlight to get /usr/share/doc/git/contrib/diff-highlight/diff-highlight (see this commit)