4

I made some changes in my source file. When i try to see the diff using

git diff src/core/src/authentication.rs

The diff on the console is having ^M appended to all the lines I edited.

+            Some(b) => {^M
+                Ok(b)^M
+            }^M
+        }^M
     }

I checked out in notepad to see if I can see something there and clean it up but was not visible there with any encoding I tried. I read somewhere that it is a \r but not sure. Could not replace/find it in vim. However this does not happen in all the files I see a diff for. Why is this happening? I guess this is not part of my source code and only something to do with git.

Edit: I use vim as my editor.

1 Answer 1

5

The primary reason is character sequences used to represent line breaks. In windows, a line break is represented by \r\n. So, when we edit a text (created on windows) on a Unix/Linux editor like VIM, the editor sees an extra character (since line break in UNIX is \n) and tries to render it. The output turns out to be ^M.

One way to deal with it on vim is

:set ff=unix
:wq

On a different note, its a standard practice as mentioned in github docs to use "\n" as a new line character in git based repos. In order to set that by default, add this to your global git config settings.

$git config --global core.autocrlf true

Hope this helps.

1
  • That did help. And yes I might have used Windows as well. Commented Jul 26, 2018 at 5:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.