In my case I was working on a project which was developed in Windows and I used OS X. When I changed some code, I saw ^M
at the end of the lines I added in git diff
. I think the ^M
were showing up because they were different line endings than the rest of the file. Because the rest of the file was developed in Windows it used CR
line endings, and in OS X it uses LF
line endings.
If you go for this option, you should however fix the current files (because they're still using the CR
line endings). I did this by following these steps:
Remove all files from the repository, but not from your filesystem.
git rm --cached -r .
Add a .gitattributes
file that enforces certain files to use a LF
as line endings. Put this in the file:
*.ext text eol=crlf
Replace .ext
with the file extensions you want to match.
Add all the files again.
git add .
This will show messages like this:
warning: CRLF will be replaced by LF in <filename>.
The file will have its original line endings in your working directory.
You could remove the .gitattributes
file unless you have stubborn Windows users that don't want to use the "Checkout Windows-style, commit Unix-style line endings" option.
Commit and push it all.
Remove and checkout the applicable files on all the systems where they're used. On the Windows systems, make sure they now use the "Checkout Windows-style, commit Unix-style line endings" option. You should also do this on the system where you executed these tasks because when you added the files git said:
The file will have its original line endings in your working directory.
You can do something like this to remove the files:
git ls | grep ".ext$" | xargs rm -f
And then this to get them back with the correct line endings:
git ls | grep ".ext$" | xargs git checkout
Of course replacing .ext
with the extension you want.