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

GitLine Endings

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

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

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.

Solution 2 - Git

^M started appearing everywhere for me because I didn't have npm installed on my system (linux sub system)

Once I installed npm and did sudo apt update, all the ^M went away.

Not sure if that's how you encountered it but this is a possible solution.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMooView Question on Stackoverflow
Solution 1 - GitmelpomeneView Answer on Stackoverflow
Solution 2 - GitZaneKView Answer on Stackoverflow