What's the best CRLF (carriage return, line feed) handling strategy with Git?

后端 未结 9 1774
青春惊慌失措
青春惊慌失措 2020-11-22 07:30

I tried committing files with CRLF-ending lines, but it failed.

I spent a whole work day on my Windows computer trying different strategies and was almost drawn to s

9条回答
  •  逝去的感伤
    2020-11-22 08:19

    These are the two options for Windows and Visual Studio users that share code with Mac or Linux users. For an extended explanation, read the gitattributes manual.

    * text=auto

    In your repo's .gitattributes file add:

    *   text=auto
    

    This will normalize all the files with LF line endings in the repo.

    And depending on your operating system (core.eol setting), files in the working tree will be normalized to LF for Unix based systems or CRLF for Windows systems.

    This is the configuration that Microsoft .NET repos use.

    Example:

    Hello\r\nWorld
    

    Will be normalized in the repo always as:

    Hello\nWorld
    

    On checkout, the working tree in Windows will be converted to:

    Hello\r\nWorld
    

    On checkout, the working tree in Mac will be left as:

    Hello\nWorld
    

    Note: If your repo already contains files not normalized, git status will show these files as completely modified the next time you make any change on them, and it could be a pain for other users to merge their changes later. See refreshing a repository after changing line endings for more information.

    core.autocrlf = true

    If text is unspecified in the .gitattributes file, Git uses the core.autocrlf configuration variable to determine if the file should be converted.

    For Windows users, git config --global core.autocrlf true is a great option because:

    • Files are normalized to LF line endings only when added to the repo. If there are files not normalized in the repo, this setting will not touch them.
    • All text files are converted to CRLF line endings in the working directory.

    The problem with this approach is that:

    • If you are a Windows user with autocrlf = input, you will see a bunch of files with LF line endings. Not a hazard for the rest of the team, because your commits will still be normalized with LF line endings.
    • If you are a Windows user with core.autocrlf = false, you will see a bunch of files with LF line endings and you may introduce files with CRLF line endings into the repo.
    • Most Mac users use autocrlf = input and may get files with CRLF file endings, probably from Windows users with core.autocrlf = false.

提交回复
热议问题