Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to avoid git-apply changing line endings

Tags:

git

newline

eol

I have a git repo set with core.eol=crlf, core.autocrlf=true and core.safecrlf=true.

When I apply a patch from another crlf repo and to my repo all the line endings for the effected file are changed to lf. Currently I'm applying the patch as so:

git apply --ignore-whitespace mychanges.patch

(It seems I have to use --ignore-whitespace to get the patch to successfully apply.)

My current work around is to run unix2dos on the file. Is there a better way of getting apply to conform to my eol settings?

like image 457
m0tive Avatar asked Jun 10 '11 15:06

m0tive


2 Answers

I would not allow my source control system to control my line endings. Auto crlf is false and showing diffs without the annoying ^M is done by setting core.whitespace to cr-at-eol. Now diff output will be nicer to read.

like image 147
Adam Dymitruk Avatar answered Oct 15 '22 08:10

Adam Dymitruk


Check if the issue persists with Git 2.14.x/2.15 (Q3 2015)

See commit c24f3ab (19 Aug 2017), and commit 2fea9de (13 Aug 2017) by Torsten Bögershausen (tboegi).
(Merged by Junio C Hamano -- gitster -- in commit a17483f, 27 Aug 2017)

apply: file committed with CRLF should roundtrip diff and apply

When a file had been committed with CRLF but now .gitattributes says "* text=auto" (or core.autocrlf is true), the following does not roundtrip, git apply fails:

printf "Added line\r\n" >>file &&
git diff >patch &&
git checkout -- . &&
git apply patch

Before applying the patch, the file from working tree is converted into the index format (clean filter, CRLF conversion, ...).
Here, when commited with CRLF, the line endings should not be converted.

like image 22
VonC Avatar answered Oct 15 '22 09:10

VonC