How should I use git diff for long lines?

GitDiffWord Wrap

Git Problem Overview


I'm running git-diff on a file, but the change is at the end of a long line.

If I use cursor keys to move right, it loses colour-coding—and worse the lines don't line up—making it harder to track the change.

Is there a way to prevent that problem or to simply make the lines wrap instead?

I'm running Git 1.5.5 via mingw32.

Git Solutions


Solution 1 - Git

Or if you use less as default pager just type -S while viewing the diff to reenable wrapping in less.

Solution 2 - Git

The display of the output of git diff is handled by whatever pager you are using.

Commonly, under Linux, less would be used.

You can tell git to use a different pager by setting the GIT_PAGER environment variable. If you don't mind about paging (for example, your terminal allows you to scroll back) you might try explicitly setting GIT_PAGER to empty to stop it using a pager. Under Linux:

$ GIT_PAGER='' git diff

Without a pager, the lines will wrap.

If your terminal doesn't support coloured output, you can also turn this off using either the --no-color argument, or putting an entry in the color section of your git config file.

$ GIT_PAGER='' git diff --no-color

Solution 3 - Git

You can also use git config to setup pager to wrap.

$ git config core.pager 'less -r' 

Sets the pager setting for the current project.

$ git config --global core.pager 'less -r' 

Sets the pager globally for all projects

Solution 4 - Git

With full credit to Josh Diehl in a comment to this answer, I nevertheless feel like this ought to be an answer unto itself, so adding it:

One way to deal with seeing differences in long lines is to use a word-oriented diff. This can be done with:

git diff --word-diff

In this case, you'll get a significantly different diff output, that shows you specifically what has changed within a line.

For example, instead of getting something like this:

diff --git a/test-file.txt b/test-file.txt
index 19e6adf..eb6bb81 100644
--- a/test-file.txt
+++ b/test-file.txt
@@ -1 +1 @@
-this is a short line
+this is a slightly longer line

You might get something like this:

diff --git a/test-file.txt b/test-file.txt
index 19e6adf..eb6bb81 100644
--- a/test-file.txt
+++ b/test-file.txt
@@ -1 +1 @@
this is a [-short-]{+slightly longer+} line

Or, with colorization, instead of this:

result of just git diff

You might get this:

result of git diff --word-diff

Now, if you're comparing a really long line, you may still have issues with the pager situation you originally described, and which has been addressed, apparently to satisfaction, in other answers. Hopefully this gives you a new tool, though, to more easily identify what on the line has changed.

Solution 5 - Git

To use less as the pager and make line wrapping permanent you can simply enable the fold-long-lines option:

git config --global core.pager 'less -+S'

This way you do not have to type it while using less.

Cheers

Solution 6 - Git

Just googled up this one. GIT_PAGER='less -r' works for me

Solution 7 - Git

Mac OSX: None of the other answers except someone45's '-S' while less is running worked for me. It took the following to make word-wrap persistent:

git config --global core.pager 'less -+$LESS -FRX'

Solution 8 - Git

Since Git 1.5.3 (Sep 2007)

a --no-pager option has been available.

git --no-pager diff

https://stackoverflow.com/q/2183900/#2183920

Example

Starting with v2.1, wrap is the default

Git v2.1 Release Notes

Solution 9 - Git

Noone pointed out this till now. Its quite simple to remember and no extra configuration needs to be done in the git config

git diff --color | less -R

Solution 10 - Git

Eight years later I find a superior answer, from https://superuser.com/questions/777617/line-wrapping-less-in-os-x-specifically-for-use-with-git-diff:

git config core.pager `fold -w 80 | less`

Now you pipe the git diff through fold, first, then to less: wrapped, less page-height is correct, keep syntax highlighting.

Solution 11 - Git

When you are using "git diff" and it's showing several pages(you see ":" at the end of the page)in this case you can type "-S" and press enter.(S should be capital). it will toggle fold long lines.

Solution 12 - Git

Not a perfect solution, but gitk and git-gui can both show this information, and have scrollbars.

Solution 13 - Git

You could simply pipe the output of git diff to more:

git diff | more

Solution 14 - Git

list the current/default config:

  $ git config --global core.pager  
    less -FXRS -x2

then update and leave out the -S like:

  $ git config --global core.pager 'less -FXR -x2'

the -S: Causes lines longer than the screen width to be chopped rather than folded.

Solution 15 - Git

When in trouble, I often resort to DiffMerge. Excellent diff tool that has in-line diff highlighting. Also, in the latest versions they added a mode to have an horizontal mode.

I haven't been able to configure git to use it, though. So I do have to muck around to get both versions of the file first.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPeter BoughtonView Question on Stackoverflow
Solution 1 - Gitsomeone45View Answer on Stackoverflow
Solution 2 - GitSpoonMeiserView Answer on Stackoverflow
Solution 3 - GitShoanView Answer on Stackoverflow
Solution 4 - GitlindesView Answer on Stackoverflow
Solution 5 - GitDaniel MontezanoView Answer on Stackoverflow
Solution 6 - GitsingingfishView Answer on Stackoverflow
Solution 7 - GitJohn LembergerView Answer on Stackoverflow
Solution 8 - GitZomboView Answer on Stackoverflow
Solution 9 - GitinfocloggedView Answer on Stackoverflow
Solution 10 - GitThomson ComerView Answer on Stackoverflow
Solution 11 - GitAminView Answer on Stackoverflow
Solution 12 - GitPeter BoughtonView Answer on Stackoverflow
Solution 13 - GitAnonTidbitsView Answer on Stackoverflow
Solution 14 - Gituser5870226View Answer on Stackoverflow
Solution 15 - GitwebmatView Answer on Stackoverflow