Ignore any blank space or line break in git-diff

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 10:05:52

git diff supports comparing files line by line or word by word, and also supports defining what makes a word. Here you can define every non-space character as a word to do the comparison. In this way, it will ignore all spaces including white-spcae, tab, line-break and carrige-return as what you need.

To achieve it, there's a perfect option --word-diff-regex, and just set it --word-diff-regex=[^[:space:]]. Refer to doc for detail.

git diff --no-index --word-diff-regex=[^[:space:]] <file1> <file2>

Here's an example. I created two files, with a.html as follows:

<html><head><title>TITLE</title><meta>

With b.html as follows:

<html>
    <head>
        <title>TI==TLE</title>
        <meta>

By running

git diff --no-index --word-diff-regex=[^[:space:]] a.html b.html

It highlights the difference of TITLE and TI{+==+}TLE in the two files in plain mode as follows. You can also specify --word-diff=<mode> to display results in different modes. The mode can be color, plain, porcelain and none, and with plain as default.

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>

This does the trick for me:

git diff --ignore-blank-lines
Mudassir Razvi

git-diff compares files line by line

It checks the first line of your file1 with that in file2, since they are not same it reports an error.

Ignoring white space means that foo bar will match foobar if on the same line. Since your files span multiple lines in one and only one line in other, the files will always differ

If you really want to check that the files contain the exact same non-whitespace characters, you could try something like this:

diff <(perl -ne 's/\s*//xg; print' file1) <(perl -ne 's/\s*//g; print' file2)

Hope it solves your problem!

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!