33

如何,为 git diff --word-diff 添加单词分隔符?

例如,我希望差异能够识别

function(A,B,C) -> function(A, B, C)

只添加了空格而不是替换整行。

4

3 回答 3

41

使用--word-diff-regex

   --word-diff-regex=<regex>
       Use <regex> to decide what a word is, instead of considering runs
       of non-whitespace to be a word. Also implies --word-diff unless it
       was already enabled.

       Every non-overlapping match of the <regex> is considered a word.
       Anything between these matches is considered whitespace and
       ignored(!) for the purposes of finding differences. You may want to
       append |[^[:space:]] to your regular expression to make sure that
       it matches all non-whitespace characters. A match that contains a
       newline is silently truncated(!) at the newline.

       The regex can also be set via a diff driver or configuration
       option, see gitattributes(1) or git-config(1). Giving it explicitly
       overrides any diff driver or configuration setting. Diff drivers
       override configuration settings.

我从未使用过它,但我想这git diff --word-diff-regex="[^[:space:],]+"可能会奏效。

于 2012-05-07T13:32:09.167 回答
11

除了 KurzedMetal 的回答:如果没有引号,它的效果会更好,因为它在点击 TAB 时不会破坏我的 bashs 自动完成。

git diff --word-diff-regex=[^[:space:],]+

于 2013-10-10T11:01:33.950 回答
2

您也可以尝试-w/ --ignore-all-space,尽管这可能会忽略比您喜欢的更多的空格。

于 2012-05-07T13:43:35.480 回答