255

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.

Michael
  • 8,362
  • 6
  • 61
  • 88
Peter Boughton
  • 110,170
  • 32
  • 120
  • 176
  • 9
    You may want to try 'git diff --color-words', it doesn't solve the scrolling problem, but word changes are surrounded by context on a single line! – Kevin Apr 23 '14 at 21:34
  • 10
    Using "fold" seems to work pretty well: `git diff --color-words | fold` – Amy Apr 07 '15 at 18:01
  • 1
    @Amy I've tried using `fold` but it removes color. Since you specify `--color-words` I assume you managed to pass colors to `fold`. How? – Nero gris Feb 14 '20 at 23:02
  • @Nerogris You're right. Maybe at the time when this was written (2015) it worked better? – Amy Jan 10 '22 at 00:16
  • Maybe color is OS dependent? I'm seeing it on a Mac. – Mark Chackerian Oct 13 '22 at 21:43

15 Answers15

240

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

Nakilon
  • 34,866
  • 14
  • 107
  • 142
someone45
  • 2,409
  • 2
  • 14
  • 2
  • 70
    related tip, use `--word-diff` to see a color-coded highlighting of changed words – Josh Diehl Dec 06 '12 at 06:17
  • 5
    just a note for this since i saw some people were having issues with it, -S is different from -s (make sure you're hitting shift+s) – longda Apr 04 '13 at 18:46
  • 3
    @JoshDiehl: I hope you don't mind... I think the `--word-diff` part deserves to have its own answer on this, as it strikes me as doing a lot to help solve the underlying issue: figuring out what's changed on a long line. So, I've made it one: http://stackoverflow.com/a/19253759/313756 – lindes Oct 08 '13 at 16:57
  • 1
    Note: this does not seem to work on OS X (mavericks). – DilithiumMatrix Jul 06 '14 at 15:53
  • @zhermes It works for me in Mavericks, with less 418. Make sure you are typing `-S` with a capital S, not `-s`. Less displays a message “Fold long lines (press RETURN)” at the bottom after I type `-S`, and then pressing Return enables wrapping. – Rory O'Kane Oct 14 '14 at 18:16
  • See [Daniel Montezano's answer](http://stackoverflow.com/a/12499930/1242922) for enabling wrapping as default instead of typing it every time. – Richard Kiefer Mar 24 '15 at 18:34
  • 3
    This is the right answer, but I kept trying to add `-S` to the command or to the pager core default...for anyone like me that couldn't **read the entire, brief solution** you **type `-S` *while viewing the diff* not before while issuing the command** – WEBjuju Apr 11 '17 at 15:06
131

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
Zombo
  • 1
  • 62
  • 391
  • 407
SpoonMeiser
  • 19,918
  • 8
  • 50
  • 68
  • 1
    I can confirm that setting GIT_PAGER to blank does cause the lines to wrap. It also inserts symbols making it a bit difficult to read, but if necessary I can find a different pager, so still a valid answer. :) Thanks. – Peter Boughton Sep 30 '08 at 20:00
  • 1
    What symbols does it add that make it difficult to read? I might be able to edit my answer to solve that problem too. – SpoonMeiser Sep 30 '08 at 21:33
  • Mostly "<-[m" for each newline (where <- was a single arrow character), but also markers where (I think) each colour would have started, like "<-[1m" and "<-[32m". – Peter Boughton Sep 30 '08 at 22:42
  • 1
    Does the --no-color argument help at all? I'm not sure about the newline characters. – SpoonMeiser Oct 01 '08 at 08:48
  • Yes, thanks, that prevents all the unwanted content appearing, so the newline ones must have been colour related also. – Peter Boughton Oct 01 '08 at 18:13
121

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

Shoan
  • 4,003
  • 1
  • 26
  • 29
  • 3
    with msysgit(1.8.1.msysgit.1) it worked for me using double quotes - `git config --global core.pager "less -r"` – kerim Dec 12 '13 at 05:52
  • This got me wrapping permanently with git diff on OS X. thanks! – Thomson Comer Jan 17 '14 at 17:14
  • 1
    It works, but I don't understand why. Can someone explain? `man less`, `-r` says nothing about wrapping. – Ciro Santilli OurBigBook.com Jul 05 '14 at 09:35
  • @ThomsonComer '-r' pertains to showing control characters in OS X... How did you get this working? – DilithiumMatrix Jul 06 '14 at 15:54
  • 1
    I can't remember for sure now. But found some links that explain it more: http://michael.otacoo.com/linux-2/avoid-escape-characters-in-git/ http://superuser.com/questions/366930/how-do-i-get-the-git-pager-to-act-normal-less-leaves-output-on-the-screen-when http://unix.stackexchange.com/questions/19317/can-less-retain-colored-output – Shoan Jul 09 '14 at 00:25
  • Hey @zhermes, I don't know why it works, but it does. I confirmed it again today on OS X. Setting the pager to less -r on OS X causes wrapping to occur, seeing the pager to simply less causes long lines to run off of the screen. – Thomson Comer Oct 01 '14 at 16:54
  • See my improved answer near the bottom. :) `git config core.pager 'fold -w 80 | less'` – Thomson Comer Feb 11 '16 at 23:22
  • I used `'less -S'` instead and it worked for what I wanted (no wrapping lines) – Shujito Mar 08 '18 at 18:10
68

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.

Michael
  • 8,362
  • 6
  • 61
  • 88
lindes
  • 9,854
  • 3
  • 33
  • 45
  • 6
    There are a couple options for `--word-diff`: `color`,`plain`, and `porcelain`. Also, one can change the regex for word boundaries with `--word-diff-regex`. The default appears to be `\S+`. ([v2.1.1](http://git-scm.com/docs/git-diff/2.1.1)) – Michael Oct 02 '14 at 20:55
  • 3
    `--color-words` looks to be shorthand for `--word-diff=color`, which is nice when you're just viewing the diff, not sharing it. – CivFan Dec 04 '15 at 23:30
  • Ah, this is interesting and potentially very useful. Thanks for sharing! – Darragh Enright Jul 11 '18 at 16:34
  • 3
    I like `--word-diff=porcelain` better than `--word-diff` because `porcelain` will line up the changes on separate lines, whereas `--word-diff` put the changes inline. Separate lines allow you to see the differences more easily when the differences are subtle. – wisbucky Nov 18 '19 at 22:46
  • 1
    Life changing for LaTeX files! – Caleb Stanford May 13 '20 at 23:39
30

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

19

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'
John Lemberger
  • 2,689
  • 26
  • 25
  • This worked for me too, but I don't understand why. What is the `-+$LESS` parameter doing? Unless git defines it, my LESS environment variable isn't even set. – jakar Aug 10 '12 at 18:00
  • 3
    @jakar: then it does nothing. In some environments, `$LESS` is set to some value (e.g. by a `.login` or a `.profile` or something), and that option, I think, just adds the defaults, and then adds `-FRX` on top of those. – naught101 Dec 04 '12 at 11:51
  • This **did not** work for me on OS X Mavericks, `$LESS` is undefined. – DilithiumMatrix Jul 06 '14 at 15:55
  • naught101 is correct that +-$LESS simply includes whatever settings are in .login or .profile (if any). It's working fine on Mavericks for me, but you can omit it, if it causes problems. – John Lemberger Jul 07 '14 at 16:32
19

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

singingfish
  • 3,136
  • 22
  • 25
  • 10
    Even better (for me): `less -R` (Actually, I use `less -eiFRSX`, which solves color and line-wrap problems too.) – cdunn2001 Sep 22 '11 at 02:08
  • 2
    less -R is like -r, but only ANSI "color" escape sequences are output in "raw" form. Unlike -r, the screen appearance is maintained correctly in most cases. (man less) – richk Sep 26 '14 at 12:21
12

Since Git 1.5.3 (Sep 2007)

a --no-pager option has been available.

git --no-pager diff

How do I prevent git diff from using a pager?

Example

Starting with v2.1, wrap is the default

Git v2.1 Release Notes

Community
  • 1
  • 1
Zombo
  • 1
  • 62
  • 391
  • 407
  • 1
    The Release Notes link has helped me out. Just wanted to know what is going (wr)on(g) in git > 2 ^^ – func0der Oct 14 '15 at 09:41
6

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
infoclogged
  • 3,641
  • 5
  • 32
  • 53
  • By far the simplest of all the answers in this page. My environment - Oracle Linux 7.6. `git diff --color | less -FR` for small changes when do not need to scroll – Rakesh N Sep 03 '19 at 11:40
5

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.

Community
  • 1
  • 1
Thomson Comer
  • 3,919
  • 3
  • 30
  • 32
5

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.

Amin
  • 231
  • 4
  • 10
3

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

Zombo
  • 1
  • 62
  • 391
  • 407
Peter Boughton
  • 110,170
  • 32
  • 120
  • 176
3

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

git diff | more
1

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.

sKhan
  • 9,694
  • 16
  • 55
  • 53
-1

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.

webmat
  • 58,466
  • 12
  • 54
  • 59