How should I use git diff for long lines?
Asked Answered
S

15

259

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.

Secretarygeneral answered 25/9, 2008 at 21:12 Comment(5)
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!Eamon
Using "fold" seems to work pretty well: git diff --color-words | foldLegault
@Legault I've tried using fold but it removes color. Since you specify --color-words I assume you managed to pass colors to fold. How?Lively
@Nerogris You're right. Maybe at the time when this was written (2015) it worked better?Legault
Maybe color is OS dependent? I'm seeing it on a Mac.Petrol
W
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
Weathercock answered 30/9, 2008 at 10:43 Comment(5)
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.Secretarygeneral
What symbols does it add that make it difficult to read? I might be able to edit my answer to solve that problem too.Weathercock
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".Secretarygeneral
Does the --no-color argument help at all? I'm not sure about the newline characters.Weathercock
Yes, thanks, that prevents all the unwanted content appearing, so the newline ones must have been colour related also.Secretarygeneral
H
242

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

Handpick answered 24/6, 2010 at 6:58 Comment(7)
related tip, use --word-diff to see a color-coded highlighting of changed wordsKoester
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)Terchie
@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: https://mcmap.net/q/109243/-how-should-i-use-git-diff-for-long-linesRintoul
Note: this does not seem to work on OS X (mavericks).Linville
@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.Crellen
See Daniel Montezano's answer for enabling wrapping as default instead of typing it every time.Judicator
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 commandMaximalist
W
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
Weathercock answered 30/9, 2008 at 10:43 Comment(5)
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.Secretarygeneral
What symbols does it add that make it difficult to read? I might be able to edit my answer to solve that problem too.Weathercock
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".Secretarygeneral
Does the --no-color argument help at all? I'm not sure about the newline characters.Weathercock
Yes, thanks, that prevents all the unwanted content appearing, so the newline ones must have been colour related also.Secretarygeneral
A
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

Alerion answered 9/9, 2010 at 4:46 Comment(8)
with msysgit(1.8.1.msysgit.1) it worked for me using double quotes - git config --global core.pager "less -r"Aesculapius
This got me wrapping permanently with git diff on OS X. thanks!Weatherford
It works, but I don't understand why. Can someone explain? man less, -r says nothing about wrapping.Ivories
@ThomsonComer '-r' pertains to showing control characters in OS X... How did you get this working?Linville
I can't remember for sure now. But found some links that explain it more: michael.otacoo.com/linux-2/avoid-escape-characters-in-git superuser.com/questions/366930/… unix.stackexchange.com/questions/19317/…Alerion
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.Weatherford
See my improved answer near the bottom. :) git config core.pager 'fold -w 80 | less'Weatherford
I used 'less -S' instead and it worked for what I wanted (no wrapping lines)Conde
R
72

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.

Rintoul answered 8/10, 2013 at 16:53 Comment(5)
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)Circumscissile
--color-words looks to be shorthand for --word-diff=color, which is nice when you're just viewing the diff, not sharing it.Frigate
Ah, this is interesting and potentially very useful. Thanks for sharing!Organogenesis
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.Friendly
Life changing for LaTeX files!Filial
E
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

Eurydice answered 19/9, 2012 at 17:32 Comment(2)
Worked for me on OS X 10.9.5. Thanks!Bullyrag
Similarly, if you want to DISABLE line wrapping when reading Git diffs, the setting is similarly: git config --global core.pager 'less -SHandwoven
T
19

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

Toggery answered 21/5, 2009 at 2:32 Comment(2)
Even better (for me): less -R (Actually, I use less -eiFRSX, which solves color and line-wrap problems too.)Failing
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)Spokeshave
T
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'
Tinny answered 23/5, 2011 at 21:57 Comment(4)
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.Feleciafeledy
@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.Eagre
This did not work for me on OS X Mavericks, $LESS is undefined.Linville
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.Tinny
U
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

Upheaval answered 13/5, 2014 at 23:10 Comment(1)
The Release Notes link has helped me out. Just wanted to know what is going (wr)on(g) in git > 2 ^^Uchida
F
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
Fusco answered 30/6, 2016 at 14:18 Comment(1)
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 scrollSubaxillary
W
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.

Weatherford answered 11/2, 2016 at 23:21 Comment(0)
G
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.

Garygarza answered 11/3, 2017 at 20:10 Comment(2)
this is way easier than having to mess with any settingsAlumina
In my version of "less" (or whatever), pressing enter is not needed. Type -S at the prompt, and the display changes to wrapped lines immediately.Portecochere
S
3

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

Secretarygeneral answered 25/9, 2008 at 21:48 Comment(0)
R
3

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

git diff | more
Registration answered 11/7, 2013 at 15:7 Comment(0)
S
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.

Spandrel answered 1/2, 2016 at 23:58 Comment(0)
L
-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.

Lisandralisbeth answered 26/9, 2008 at 11:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.