Search code examples
gitgitattributestexmaker

How to configure .gitattributes s.t. files are not shown as dirty when comitting with LF on windows?


I want to configure the .gitattributes in a repository to resolve EOL issues for all users without them needing to change their local git configuration.

My .gitattributes looks like this:

* text=auto

*.pdf binary
*.xlsx binary
*.png binary
*.jpg binary

When checking out on Windows, as expected, the line endings in the working tree are CRLF and the line endings in the index are LF:

i/lf    w/crlf  attr/text=auto          sample.tex

However, Texmaker tends to change the line endings of a tex file to Unix-style LF when saved, even if no actual changes occurred. After "editing", hence, both the working tree and the index are LF:

i/lf    w/lf    attr/text=auto          sample.tex

Unfortunately, git status will now show the file as dirty, even though no actual changes occurred. How can I resolve this problem?

I know that I could enforce a checkout with Unix-style endings through eol=lf, but then other users may run into problems if their editor enforces CRLF. I have set core.autocrlf=true. I'm not sure whether this adds to the issue. However, I would also like to find a general (repository-based) solution for this problem, instead of changing local configurations.


Solution

  • There's no way to avoid this file showing up as modified if it has in fact been modified. The reason is that Git uses the index, which keeps file size, among other data, to see if a file has been modified. If the file size changes, Git will mark it as modified, even if it won't result in an actual change to the stored blob.

    The reason is that the index is an optimization, and it would be extremely expensive to process the file through the conversion mechanism as part of git status. For example, if you have several large files (say 500 MB) that are all using line-ending conversion, you'd spend a lot of time performing conversions, making git status very slow.

    However, having said that, if you'd prefer your TeX files to always have LF endings for all users, you can do *.tex text eol=lf. If you want to use LF for all files, that would be * text=auto eol=lf.

    Or, if it's just for you, you can adjust core.eol to be lf, like so: git config --global core.eol lf (or just for this repo with --global), which will disable CRLF endings for all files. You could also edit .git/info/attributes and put *.tex text eol=lf, which will configure things for just TeX files and will override, but not modify, the .gitattributes in the repository.

    In my experience, most editors are capable of handing just LF endings these days, even on Windows, so that may be a valid choice. Asking developers to configure their editors to always produce LF is reasonable in many circumstances, and if you can get agreement on that approach, then it may make it easier to solve this in a general way.