忽略git-diff中的空白或换行符 [英] Ignore any blank space or line break in git-diff

查看:4866
本文介绍了忽略git-diff中的空白或换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用两种不同的方式呈现相同的文件,并希望使用 git diff 进行比较,照顾忽略每个空格,制表符,行



我正在尝试这样做:



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



但是当一些html标签全部折叠在一行上时(而不是每行一个和列表)

 < html>< / c>检测是否有区别(对我而言不是) head>< title> TITLE< / title>< meta ...... 

与不同

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

我错过了什么选择来完成我需要和威胁一样吗?

c $ c>支持逐行或逐字比较文件,还支持定义什么是单词。在这里,您可以将每个非空格字符定义为一个单词来进行比较。通过这种方式,它将忽略所有空间,包括白色的spcae,tab,line-break和carrige-return作为你需要的东西。

为了达到它,有一个完美的选项 - word-diff-regex ,并将其设置为 - word-diff-regex = [^ [:space: ] 。详情请参阅文档

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

下面是一个例子。我创建了两个文件,其中 a.html 如下:

 << ; HTML>< HEAD><标题> TITLE< /标题><元> 



b.html 如下:

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

运行

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

它强调了 TITLE TI {+ == +} TLE plain 模式下的两个文件中,如下所示。您还可以指定 - word-diff =< mode> 以不同模式显示结果。 模式可以是 color , plain ,瓷器,并以 plain 作为默认值。

  diff --git a / d.html b / a.html 
索引df38a78..306ed3e 100644
- - a / d.html
+++ b / a.html
@@ -1 +1,4 @@
< html>
< head>
< title> TI {+ == +} TLE< / title>
< meta>


I have the same file rendered in two different ways and want to compare it using git diff, taking care of ignoring every white-space, tab, line-break, carriage-return, or anything that is not strictly the source code of my files.

I'm actually trying this:

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

but when some html tags are collapsed all on one line (instead of one per line and tabulated) git-diff detect is as a difference (while for me it is not).

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

is different from

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

What option do I miss to accomplish what I need and threat as if it was the same?

解决方案

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>

这篇关于忽略git-diff中的空白或换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆