I've seen a couple of posts on here talking about how ^M
appears in diff's. Indeed, if I go on GitHub, run git show
or git diff ...
I see the entire file is changed, and it is treated as a single line.
My colleague works on Mac, and I am on Windows, and I understand that the Windows line-endings are the problem, so you need to follow github's instructions and run
$git config --global core.autocrlf true
Which I did. Now, when I commit files using a text editor, I see
hint: Waiting for your editor to close the file... unix2dos: converting file full_path/.git/COMMIT_EDITMSG to DOS format...
They also say run:
$git add . -u
$git commit -m "Saving files before refreshing line endings"
$git add --renormalize .
$git commit -m "Normalize all the line endings"
But the problem is this does nothing. It just says my working tree is clean.
Now, if I run:
$git config --global core.autocrlf true
and then immediately add 2 new lines to one of my files, adding and committing this file still leaves me with the big jumbled mess and the changes being interpreted as happening to the entire file. I still see ^M
everywhere.
I've seen this answer as well, and it says to run
# Remove everything from the index
$git rm --cached -r .
# Re-add all the deleted files to the index
# You should get lots of messages like: "warning: CRLF will be replaced by LF in <file>."
$git diff --cached --name-only -z | xargs -0 git add
# Commit
$git commit -m "Fix CRLF"
But the line starting with git diff
just says "Nothing specified, nothing added." I tried writing:
$git diff --cached --name-only -z | xargs -0 git add .
But still, a git status
showed there was no difference.
So then, I ran the next string of commits:
- First commit: delete test.txt
- Second commit: checkout the previous version of test.txt and commit it
- Add 2 lines of text to test.txt
Now, git show should show me the difference between test.txt and the version with 2 less lines: but there is still that ^M
. This is despite the fact that it says
hint: Waiting for your editor to close the file... unix2dos: converting file full_path/.git/COMMIT_EDITMSG to DOS format...
So clearly, it is at least converting my edit message, but why are none of these solutions working for me? Why won't git actually convert my line endings?
Edit
I've now also tried: uninstalling Git, deleting the folder in my program files, and deleting .gitconfig at a global scale, then reinstalling with the option "checkout Windows style, commit Unix style line endings." But this did not work.
Manually going in and replacing the line endings with "\n" (through find and replace) has worked, and now git diff
is cleanly displayed, but I'd like a way to ensure my projects never face this issue again, because noticing it required actually looking at the line endings which by default I do not show.
Note that adding *.txt eol=lf
to .gitattributes in my repository and ensuring the filename is exactly .gitattributes with no extension works on a project by project basis.