How should I use git diff for long lines?

后端 未结 15 2420
庸人自扰
庸人自扰 2020-12-04 05:37

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 do

相关标签:
15条回答
  • 2020-12-04 05:57

    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.

    0 讨论(0)
  • 2020-12-04 06:05

    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 <code>git diff</code>

    You might get this:

    result of <code>git diff --word-diff</code>

    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.

    0 讨论(0)
  • 2020-12-04 06:05

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

    git diff | more
    
    0 讨论(0)
  • 2020-12-04 06:06

    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'
    
    0 讨论(0)
  • 2020-12-04 06:07

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

    0 讨论(0)
  • 2020-12-04 06:08

    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

    0 讨论(0)
提交回复
热议问题