gitline-endings

What is the meaning of ^M in 'git diff'?


I am diffing a file between forked and upstream Bitbucket repositories:

$ git diff origin/branchA..upstream/branchB -- some/file/path.xyz

It seems to return the same difference for almost every file:

-<U+FEFF>@using Sitecore.Mvc
+@using Sitecore.Mvc^M

What is the exact meaning of ^M that only shows up after the first line? I see this issue when I compare other files as well. I am on a Windows Server 2008 R2 machine. core.autocrlf is set to true. The .gitattributes is set to text eol=lf. My Git version is 2.5.1.windows.1.


Solution

  • ^M represents carriage return (CR). This diff means something removed a Unicode BOM from the beginning of the line and added a CR at the end.

    If ^M is in the new-changes side (right/green side), then that means the file's line-endings were changed from LF to CRLF, otherwise, file was CRLF and is now changed to LF.

    The ^ symbol stands for Control, so ^M means Ctrl+M.

    To get from that to the actual ASCII character code, you take the base character and flip bit 6 (i.e. XOR with 64). For letters, that just means subtract 64. So e.g. ^A is character code 1 (because A is 65). ^M is 77 - 64 = 13 (because M is 77), which corresponds to carriage return in ASCII.


    You may also already know:

    ^L represents the form-feed character (FF is the abbreviation, not hexadecimal), and has ASCII value 12 or 0x0C.

    See also What are carriage return, linefeed, and form feed?