How to use --color-words with git add --patch?

前端 未结 7 1819
梦谈多话
梦谈多话 2020-12-07 17:27

When diffing files, I prefer to use git diff --color-words. Is there a way to make this the default format for diffs when using git add --patch or

7条回答
  •  死守一世寂寞
    2020-12-07 18:15

    Taking cue from VonC's answer. Here are detailed steps to use --interactive option introduced in git 2.9.

    Add diff-highlight to your PATH.

    On Ubuntu, diff-highlight comes with git and can be found in /usr/share/git/diff-highlight/diff-highlight.

    Otherwise, you can download and set it up manually.

    cd ~/bin
    curl -LO "https://raw.githubusercontent.com/git/git/master/contrib/diff-highlight/diff-highlight"
    chmod u+x diff-highlight
    

    Restart your shell, if necessary.

    Then configure Git to filter your diffs whenever it's showing them in a pager:

    git config --global pager.log 'diff-highlight | less'
    git config --global pager.show 'diff-highlight | less'
    git config --global pager.diff 'diff-highlight | less'
    git config --global interactive.diffFilter diff-highlight
    

    This will put an extra emphasis on the changed part of a line, which is almost same as --word-diff.

    The advantage is you get word diff every where, like git log --patch or git add -p.

提交回复
热议问题