Git diff没有任何意义

When I run git diff on a PHP file that I have modified, it shows me the wrong line being modified and the other lines are 'unmodified' (no changes), even though I have modified the other lines.

An example: git diff result

This is the change I have made to the document: enter image description here

It only occurs on certain files and on other occasions, it will sometimes add the other lines, but it does not mark the appropriate line deletions. On other occasions, it will just append the changes at the end of the file, without marking it as an addition.

On SourceTree, it seems that the same diff does not make any sense too: enter image description here

FYI: The files were originally downloaded from a Linux server.

I found the solution, after inspecting the offending files.

It seems that the files that were giving problems had a classic Macintosh CR only end of lines (EOLs) format. Contrast this to Window's CR+LF or UNIX's (and derivatives, including OS X) LF. Converting the file to Windows/UNIX EOL format, committing the fixed EOL files remedied the problem.

Testing it out with a test Git repository proved my suspicions right.

It might happen to be Git's way of tracking file changes, only supporting CR in combination with LF (CR+LF) and not CR alone (as what classic Mac uses).

this is happening sometimes when your file is encoding with windows new line, on git it is encoded with unix new line, that's why you migth get this error, you have to configure your line endings to unix

EDIT :

this link here could be usefull. Like he said in this post you can try depends on your editor to do :

  • For Vim users, you’re all set out of the box! Just don’t change your eol setting. For Emacs users, add (setq require-final-newline t) to your .emacs or .emacs.d/init.el file.
    • For TextMate users, you can install the Avian Missing Bundle and add TM_STRIP_WHITESPACE_ON_SAVE = true to your .tm_properties file.
  • For Sublime users, set the ensure_newline_at_eof_on_save option to true.
  • For RubyMine, set “Ensure line feed at file end on Save” under “Editor.”