Git diff - ignore reorder

前提是你 提交于 2020-08-19 06:17:06

问题


git diff numbers

diff --git a/numbers b/numbers
index 5f5fbe7..d184fef 100644
--- a/numbers
+++ b/numbers
@@ -1,3 +1,3 @@
-1
+4
+3
 2
-3

Number 3 is repeated but the order is changed. Any way to ignore the reorder in git or any grep solution? I want the result of only added and deleted numbers, not reordering of the same numbers Any help?


回答1:


The diffing tools are usually implemented in terms of the Myers' diff algorithm. There isn't much that you can do to control the behaviour of GNU/git/diff. (There are a couple of switches that you can pass to diff to affect the behaviour, but in your case they are irrelevant.)

You could simply post-process the output and remove the duplicate lines, for example you can pipe your diff through the following awk script that will remove (duplicate) -/+ reordering.

git diff | awk '{ seen[substr($0,2)]++; l[i++] = $0; } END { for (j = 0; j < i; ++j) if (seen[substr(l[j],2)] < 2) print l[j] }'

For your example, the output would be,

diff --git a/numbers b/numbers
index 5f5fbe7..d184fef 100644
--- a/numbers
+++ b/numbers
@@ -1,3 +1,3 @@
-1
+4
 2



回答2:


If it is a single file, it can be done that way:

diff -u <(sort ./numbers) <(git show HEAD^^:./numbers | sort)

Running this command will tell you if a sorted file will introduce or not any other difference (i.e. ignoring lines order). If there are any suppressed or new lines, you will see them, but note that the diff output may not be accurate (files have been sorted, so these lines may be printed in a different order).



来源:https://stackoverflow.com/questions/21771313/git-diff-ignore-reorder

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