忽略git-diff中的任何空格或换行符

Kam*_*her 18 git diff git-diff

我有两个不同的方式呈现相同的文件,并希望使用它来比较它git diff,忽略每个空格,制表符,换行符,回车符,或任何不严格的文件源代码.

我其实是在尝试这个:

git diff --no-index --color --ignore-all-space <file1> <file2>

但是当一些html标签全部折叠在一条线上(而不是每条线和表格一条)时,git-diff检测是一个区别(虽然对我来说不是).

<html><head><title>TITLE</title><meta ......
Run Code Online (Sandbox Code Playgroud)

不同于

<html>
    <head>
        <title>TITLE</title>
        <meta ......
Run Code Online (Sandbox Code Playgroud)

我有什么选择可以实现我所需要的和威胁,就好像它是一样的?

Lan*_*dys 21

git diff支持逐行或逐字比较文件,还支持定义单词的内容.在这里,您可以将每个非空格字符定义为单词以进行比较.通过这种方式,它将忽略所有空间,包括白色spcae,tab,换行符和carrige-return就像你需要的那样.

要实现它,有一个完美的选择--word-diff-regex,只需设置它--word-diff-regex=[^[:space:]].有关详细信息,请参阅doc.

git diff --no-index --word-diff-regex=[^[:space:]] <file1> <file2>
Run Code Online (Sandbox Code Playgroud)

这是一个例子.我创建了两个文件,a.html如下所示:

<html><head><title>TITLE</title><meta>
Run Code Online (Sandbox Code Playgroud)

随着b.html如下:

<html>
    <head>
        <title>TI==TLE</title>
        <meta>
Run Code Online (Sandbox Code Playgroud)

通过运行

git diff --no-index --word-diff-regex=[^[:space:]] a.html b.html
Run Code Online (Sandbox Code Playgroud)

它突出的差异TITLE,并TI{+==+}TLE在这两个文件plain模式,如下所示.您还可以指定--word-diff=<mode>以不同模式显示结果.该modecolor,plain,porcelainnone,并用plain默认.

diff --git a/d.html b/a.html
index df38a78..306ed3e 100644
--- a/d.html
+++ b/a.html
@@ -1 +1,4 @@
<html>
    <head>
            <title>TI{+==+}TLE</title>
                    <meta>
Run Code Online (Sandbox Code Playgroud)

  • 不幸的是,没有显示行差异的模式,而单词差异是打开的。 (2认同)

Nar*_*shi 9

执行命令git diff --help提供了一些选项,如

--ignore-cr-at-eol
    Ignore carriage-return at the end of line when doing a comparison.

--ignore-space-at-eol
    Ignore changes in whitespace at EOL.

-b, --ignore-space-change
    Ignore changes in amount of whitespace. This ignores whitespace at line end, and considers all other sequences of one or more whitespace
    characters to be equivalent.

-w, --ignore-all-space
    Ignore whitespace when comparing lines. This ignores differences even if one line has whitespace where the other line has none.

--ignore-blank-lines
    Ignore changes whose lines are all blank.
Run Code Online (Sandbox Code Playgroud)

您可以根据需要进行组合,以下命令对我有用

git diff --ignore-blank-lines --ignore-all-space --ignore-cr-at-eol
Run Code Online (Sandbox Code Playgroud)


Dav*_*edy 7

这对我有用:

git diff --ignore-blank-lines
Run Code Online (Sandbox Code Playgroud)

  • 这会忽略空行,但如果您添加换行符将一行变成两行,则它不会忽略。 (3认同)