Search code examples
gitconsolegit-diff

git diff - show only the part of the line which is different, not entire line


I have a big single line .json files. How to use git diff to display only the part of the line that changed.

Lots of answers cover how to display changes, but this still prints entire line, and just applies different color to changed parts. I need to display changed parts only (maybe few char of context), and to omit exact matches entirely.

Example:

long line all same content except one.
long line all same content excluding one.

need something like:
-except+excluding

no context, because lines are huge

Solution

  • We have the --word-diff option for git diff. Doc here.


    Example (classic diff)

    - long line all same content except one
    + long line all same content excluding one
    

    Same with --word-diff

    long line all same content [-except-]{+excluding+} one
    

    So I guess your -except+excluding target is just one pipe to sed away:

    <your diff> | sed -Er "s/.*\[(\-[^-]+)\-\]\{(\+[^\+]+)\+\}.*/\1\2/"