Search code examples
gitline-endingseditorconfiggit-for-windows

How can I use `LF` line endings in Git for Windows in 2020 for good?


Our repository uses LF, my Git for Windows installation uses Checkout as-is, commit Unix-style line endings,

enter image description here

but I still end up with wall of errors in every file I checkout in my IDE as it still receives CRLF all the time even though it does support LF and is configured to use LF via checked in .editorconfig file (or in its own settings).:

.editorconfig
root = true

[*]
end_of_line = lf

It is 2020 already and IDEs and tooling already support LF on Windows, so how can I have the nice things too?


Solution

  • There are two git config attributes that affect the line endings: core.autocrlf and core.eol.
    Previously, you were told to use core.autocrlf = true to be able to work on cross-platform projects, but it's not true any more.

    If your system/IDE/tooling support LF and you do want to use LF as everyone else in your team without any silent lf->crlf->lf normalizations, you must turn off autocrlf and configure eol to not infer native line endings, but force it to use lf.

    Now there are two ways to achieve LF in all your files a/o repos:

    1. Globally for all repositories on your local machine.
    2. Per-repository via checked-in .gitattributes file.
      This file overrides any local configuration for everyone who clones the repo.

    I personally recommend to go with both for all local repos and to ensure cross-platform cross-dev consistency.


    1) Globally for all repositories on your local machine

    Being in your working directory (your local repo):

    1. First commit everything

    2. Let's be paranoid a bit and set it both globally and in repo as well. Just in case.

      git config --global core.eol lf
      git config --global core.autocrlf false
      
      git config core.eol lf
      git config core.autocrlf false
      
    3. Delete everything "code" except .git.
      You can also omit dependencies, installed files (such as node_modules), build files and any git-ignored file as well.

    4. and lastly run

      git reset --hard HEAD
      

    Things should be working now. Newly checked files should follow the new configuration and keep whatever line-endings were cloned from the remote repo.

    Note that if your remote repo uses mix of crlf lf endings, you will also have to run and push

    git add --renormalize .
    

    2) Per-repository via checked-in .gitattributes file

    Being in your working directory (your local repo):

    1. Create .gitattributes file in the root with this content:

      * text=auto eol=lf
      
    2. Commit the file (and everything else)

    3. Same as above

    4. Same as above

    IMPORTANT NOTE: After you introduce the file into the repository, it is necessary that everyone who still has old CRLF files does step 3 and 4 to update their working directory as just checking out the commit doesn't affect already existing files.

    git reset hard


    Notes

    setting core.autocrlf to true or input overrides core.eol

    https://www.git-scm.com/docs/git-config#Documentation/git-config.txt-coreautocrlf

    core.autocrlf = input is the preferred value on unix systems. https://stackoverflow.com/a/41282375/985454
    https://stackoverflow.com/a/4425433/985454

    Troubleshooting

    Reinstall git for windows with third option (as in the screenshot in Q)

    Checkout as is - Commit as is (core.autocrlf = false)