git-diff to ignore ^M
GitHub suggests that you should make sure to only use \n as a newline character in git-handled repos. There's an option to auto-convert:
$ git config --global core.autocrlf true
Of course, this is said to convert crlf to lf, while you want to convert cr to lf. I hope this still works …
And then convert your files:
# 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"
core.autocrlf is described on the man page.
Developing on Windows, I ran into this problem when using git tfs
. I solved it this way:
git config --global core.whitespace cr-at-eol
This basically tells Git that an end-of-line CR is not an error. As a result, those annoying ^M
characters no longer appear at the end of lines in git diff
, git show
, etc.
It appears to leave other settings as-is; for instance, extra spaces at the end of a line still show as errors (highlighted in red) in the diff.
(Other answers have alluded to this, but the above is exactly how to set the setting. To set the setting for only one project, omit the --global
.)
EDIT:
After many line-ending travails, I've had the best luck, when working on a .NET team, with these settings:
- NO core.eol setting
- NO core.whitespace setting
- NO core.autocrlf setting
- When running the Git installer for Windows, you'll get these three options:
- Checkout Windows-style, commit Unix-style line endings <-- choose this one
- Checkout as-is, commit Unix-style line endings
- Checkout as-is, commit as-is
If you need to use the whitespace setting, you should probably enable it only on a per-project basis if you need to interact with TFS. Just omit the --global
:
git config core.whitespace cr-at-eol
If you need to remove some core.* settings, the easiest way is to run this command:
git config --global -e
This opens your global .gitconfig file in a text editor, and you can easily delete the lines you want to remove. (Or you can put '#' in front of them to comment them out.)
Try git diff --ignore-space-at-eol
, or git diff --ignore-space-change
, or git diff --ignore-all-space
.