[英]Ignore any blank space or line break in git-diff
我有以兩種不同方式呈現的相同 HTML 文件,並想使用git diff
比較它,注意忽略每個空格、制表符、換行符、回車符或任何不是我的源代碼的東西文件。
我實際上正在嘗試這個:
git diff --no-index --color --ignore-all-space <file1> <file2>
但是當一些 html 標簽全部折疊在一行上(而不是每行一個並制成表格)時, git-diff檢測就不同了(而對我來說不是)。
<html><head><title>TITLE</title><meta ......
不同於
<html>
<head>
<title>TITLE</title>
<meta ......
我錯過了什么選擇來完成我的需要和威脅,就好像它是一樣的?
git diff
支持逐行或逐字比較文件,還支持定義單詞的構成。 在這里您可以將每個非空格字符定義為一個單詞來進行比較。 這樣,它會根據您的需要忽略所有空格,包括 white-spcae、tab、換行符和回車符。
要實現它,有一個完美的選項--word-diff-regex
,只需設置它--word-diff-regex=[^[:space:]]
。 有關詳細信息,請參閱文檔。
git diff --no-index --word-diff-regex=[^[:space:]] <file1> <file2>
這是一個例子。 我創建了兩個文件, a.html
如下:
<html><head><title>TITLE</title><meta>
用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>
以在不同模式--word-diff=<mode>
顯示結果。 mode
可以是color
、 plain
、 porcelain
和none
,並且默認為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>
執行命令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.
您可以根據需要進行組合,以下命令對我有用
git diff --ignore-blank-lines --ignore-all-space --ignore-cr-at-eol
這對我有用:
git diff --ignore-blank-lines
git-diff 逐行比較文件
它檢查文件 1 的第一行與文件 2 中的第一行,因為它們不同,所以會報告錯誤。
忽略空格意味着 foo bar 將匹配 foobar 如果在同一行。 由於您的文件在一行中跨越多行,而在另一行中僅跨越一行,因此文件總是不同的
如果您真的想檢查文件是否包含完全相同的非空白字符,您可以嘗試以下操作:
diff <(perl -ne 's/\s*//xg; print' file1) <(perl -ne 's/\s*//g; print' file2)
希望它能解決您的問題!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.