Search code examples
giteol

proper autocrlf setting for git


We sometimes use SourceTree as client. It handles various line endings OK, but the diff tool it uses does not. It sees all lines ending in LF as one line.

As a result, we keep all source in our repository with CRLF.

I'm looking at installing a git client. I can't find the correct setting for TEXT or AUTOCRLF. It seems like they all want to "normalize" the file on check in to LF.

I want to Convert to CRLF on checkout; and either Convert to CRLF on checkin; or Do nothing on checkin;

The best I can find so far is -text: Do nothing on checkin or checkout;

Is there any hope?

Thanks, Brad.


Solution

  • Git's native end-of-line settings are LF, and I'm not aware of any way to alter that behavior aside from recompiling it from source. However, you can force checkouts to use CRLF, which requires the creation of a .gitattributes file. For example:

    # Force C# source files to be checked out using CRLF line endings,
    # ignoring "core.eol" setting.
    *.cs eol=crlf
    
    # Don't apply CRLF conversion to PDF files, regardless of
    # user settings including "core.autocrlf".
    *.pdf -text
    
    # All other files are subjected to the usual algorithm to determine
    # whether a file is a binary file or a text file, respecting
    # "core.eol" for all files detected as text files.
    # "core.autocrlf", if set, will force the conversion to/from CRLF
    # automatically as necessary for text files.
    * text=auto
    

    Note that if you created a new file with LF line endings (e.g. using Linux, Cygwin, or an application such as Notepad++ that defaults to LF line endings), you will get a warning when adding that file to the repository if it matches a pattern in the .gitattributes file where eol=crlf is used:

    warning: LF will be replaced by CRLF in Example.cs.
    The file will have its original line endings in your working directory.
    

    Of course, if you later pull new changes, the original LF line endings will no longer be present.

    Here's a list of the interactions between various EOL/text settings:

    1. .gitattributes file
      • eol=...
        • Possible values are lf and crlf.
        • Files specified are always treated as text files and are checked out with the specified line endings.
        • Ignores core.eol and core.autocrlf.
      • text
        • Files specified are always treated as text files.
        • Respects core.eol and core.autocrlf.
      • text=auto
        • Files specified are subjected to automatic detection to determine whether they are text or binary files.
        • Respects core.eol and core.autocrlf for text files.
        • Ignores core.eol and core.autocrlf for binary files.
      • -text
        • Files specified are always treated as binary files.
        • Ignores core.eol and core.autocrlf since they don't apply to binary files.
    2. core.autocrlf configuration setting
      • input: any text files checked in will undergo conversion from CRLF to the native LF.
      • true: any text files checked in/out undergo CRLF conversion.
      • May be overridden by an eol=... rule applied to certain files in .gitattributes.
    3. core.eol configuration setting
      • native (default): any text files checked out have native line endings (CRLF on Windows, LF elsewhere)
      • lf: any text files checked out have LF line endings
      • crlf: any text files checked out have CRLF line endings
      • If core.autocrlf is set to true, this setting is ignored.
      • May be overridden by an eol=... rule applied to certain files in .gitattributes.