How to get diff working like git-diff?

前端 未结 14 2227
慢半拍i
慢半拍i 2020-12-12 10:42

I like the output formatting of git diff. The color and the +/- representation of changes between lines is easier to read than GNU di

相关标签:
14条回答
  • 2020-12-12 11:18

    This is what I suggest and it's pretty close

    diff -u FILE1 FILE2 | colordiff | less -R
    
    • colordiff: You'll have to install this
      • brew install colordiff on my Mac.
      • port install colordiff on some Macs.
      • sudo apt-get install colordiff on Debian or Ubuntu
      • For other platforms, download the source from the main page or GitHub and follow the installation instructions
    • -R: this tells Less to show colors instead of the raw codes.

    I ultimately used -w because I didn't want to see whitespace diffs.

    diff -w -u FILE1 FILE2 | colordiff | less -R
    

    Edit: As suggested by @Ciprian Tomoiaga in the comment, you can make this a function and put it in your ~/.bashrc file too.

    function gdiff () { diff -u $@ | colordiff | less -R; }
    
    0 讨论(0)
  • 2020-12-12 11:19

    GNU diff has a --color option since version 3.4 in late 2016 according to this answer on the Unix SE. That alongside -u should be enough to mimic the output of git diff:

    diff -u --color=always file1 file2 | less -r

    --color must be always when used in a pipe, auto will turn off color in pipes.

    I've only tried this with Git Bash on Windows, where less -R would only color the first line of a hunk. less -r fixed it for me in that case.

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