I noticed that when I have, in my git repository, a file with CRLF line endings, and I try to create and apply a patch which changes this file, it fails.
This is a simple way to reproduce the problem:
$ git init
$ echo -en "foo\r\nbar\r\n" > a.txt
$ git add a.txt
$ git commit -m 'first commit'
$ echo -en "abcd\r\n" >> a.txt
$ git commit -a -m 'second commit'
$ git format-patch -1 HEAD
$ git checkout HEAD~
$ git am 0001-second-commit.patch
Applying: second commit
error: patch failed: a.txt:1
error: a.txt: patch does not apply
Patch failed at 0001 second commit
hint: Use 'git am --show-current-patch' to see the failed patch
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".
What causes this problem? How can I avoid it?