`git add` adds ^M to the end of every line

Are your files being checked in from a Windows computer at any point? Windows adds CR+LF to line endings, while other OS's use LF only. If you've set core.autocrlf to false then git diff will highlight CR characters as ^M. To turn this off, you can alter the core.whitespace setting:

git config --global core.whitespace cr-at-eol

This solved this problem for me, I quote from following source: core.autocrlf explained

Hope this helps someone!

core.autocrlf

If you’re programming on Windows and working with people who are not (or vice-versa), you’ll probably run into line-ending issues at some point. This is because Windows uses both a carriage-return character and a linefeed character for newlines in its files, whereas Mac and Linux systems use only the linefeed character. This is a subtle but incredibly annoying fact of cross-platform work; many editors on Windows silently replace existing LF-style line endings with CRLF, or insert both line-ending characters when the user hits the enter key.

Git can handle this by auto-converting CRLF line endings into LF when you add a file to the index, and vice versa when it checks out code onto your filesystem. You can turn on this functionality with the core.autocrlf setting. If you’re on a Windows machine, set it to true – this converts LF endings into CRLF when you check out code:

$ git config --global core.autocrlf true