git diff, shows the same line as both deleted and

2019-03-22 16:41发布

问题:

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:

Check for spaces or line ending differences.



回答4:

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.



回答5:

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).



回答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.


标签: git git-diff