git diff, shows the same line as both deleted and added

时光毁灭记忆、已成空白 提交于 2019-12-09 07:38:16

问题


I accidently worked on the master where I had to open up a new branch.

I reverted it to its original form almost completely. At one class I get the following diff that I can not make sense out of.

index 4a9abb8..7c55879 100755
--- a/includes/site.inc.php
+++ b/includes/site.inc.php
@@ -142,11 +142,11 @@ class site{

        public $tplEngine = 'smarty';

-
+       
     private $_productsByType = array();
     private $logger;
-    protected $locale = 'tr_TR';
-
+    protected $locale = 'tr_TR';  
+    

It says that I have deleted and added the same thing, in principle there is no difference with the original index and I don't want this file to be seen as modified.

What should I do ? Thx.


回答1:


Check spaces. The replaced "empty" looking lines have spaces in them. You may also have accidentally replaced tabs with spaces or vice versa.




回答2:


It's probably whitespace changes. You can run git diff -w, which will ignore any whitespace changes.




回答3:


In addition to spaces/tabs, line endings when entered from different editors (even on Windows) and/or different OSs can contribute to diff lines showing duplicates; LF (linux/Unix) vs CRLF (Windows).




回答4:


Check for spaces or line ending differences.




回答5:


The second version of the nonblank line has a space after the semicolon and the blank lines also have different numbers of spaces.

There should be an option to have git highlight such hidden spaces so the diffs are more informative, but I don't have the manual handy.




回答6:


Note that git 1.8.4 (July 2013) will no longer show you changes with only empty lines, if you are using the new -B option.

"git diff" learned a mode that ignores hunks whose change consists only of additions and removals of blank lines, which is the same as "diff -B" (ignore blank lines) of GNU diff.

See commit 36617af7ed594d1928554356d809bd611c642dd2:

The goal of the patch is to introduce the GNU diff -B/--ignore-blank-lines as closely as possible. The short option is not available because it's already used for "break-rewrites".

When this option is used, git diff will not create hunks that simply add or remove empty lines, but will still show empty lines addition/suppression if they are close enough to "valuable" changes.

here is a more thorough description of the option:

  • real changes are interesting
  • blank lines that are close enough (less than context size) to interesting changes are considered interesting (recursive definition)
  • "context" lines are used around each hunk of interesting changes
  • If two hunks are separated by less than "inter-hunk-context", they will be merged into one.



回答7:


This can sometimes happen because the default diff algorithm used by git uses a heuristic to cut out expensive edge cases, which can result in suboptimal results.

I ran into an identical issue to this that I couldn't explain due to whitespace, line endings, etc. git diff was showing a particular line that was deleted and then re-added, as part of a hunk that was displayed as 3 lines deleted and 2 lines added but was in reality 2 lines deleted and 1 line added. Other diff programs showed the expected 2 lines deleted and 1 line added.

The file in which this happened was 4736 lines (114 KB), with diffs totaling as 640 insertions and 1340 deletions, with the hunk in question being in the middle of the file. I was unable to reduce it to a smaller test case that exhibited the same problem: making the slightest perturbations anywhere made the problem go away. My only conclusion is that I was falling into one of these edge cases of the diff algorithm's heuristic.

Git supports multiple other diff algorithms besides the default. By switching the algorithm to any of minimal, patience, or histogram, my problem went away in this case.

See also this comment in the git source code, from the embedded LibXDiff library.



来源:https://stackoverflow.com/questions/11452675/git-diff-shows-the-same-line-as-both-deleted-and-added

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