git replacing LF with CRLF

前端 未结 20 1400
眼角桃花
眼角桃花 2020-11-21 23:31

Running git on a Windows XP machine, using bash. I exported my project from SVN, and then cloned a bare repository.

I then pasted the export into the bare repositori

20条回答
  •  一生所求
    2020-11-22 00:09

    A GitHub's article on line endings is commonly mentioned when talking about this topic.

    My personal experience with using the often recommended core.autocrlf config setting was very mixed.

    I'm using Windows with Cygwin, dealing with both Windows and UNIX projects at different times. Even my Windows projects sometimes use bash shell scripts, which require UNIX (LF) line endings.

    Using GitHub's recommended core.autocrlf setting for Windows, if I check out a UNIX project (which does work perfectly on Cygwin - or maybe I'm contributing to a project that I use on my Linux server), the text files are checked out with Windows (CRLF) line endings, creating problems.

    Basically, for a mixed environment like I have, setting the global core.autocrlf to any of the options will not work well in some cases. This option might be set on a local (repository) git config, but even that wouldn't be good enough for a project that contains both Windows- and UNIX-related stuff (e.g. I have a Windows project with some bash utility scripts).

    The best choice I've found is to create per-repository .gitattributes files. The GitHub article mentions it.
    Example from that article:

    # Set the default behavior, in case people don't have core.autocrlf set.
    * text=auto
    
    # Explicitly declare text files you want to always be normalized and converted
    # to native line endings on checkout.
    *.c text
    *.h text
    
    # Declare files that will always have CRLF line endings on checkout.
    *.sln text eol=crlf
    
    # Denote all files that are truly binary and should not be modified.
    *.png binary
    *.jpg binary
    

    In one of my project's repository:

    * text=auto
    
    *.txt         text eol=lf
    *.xml         text eol=lf
    *.json        text eol=lf
    *.properties  text eol=lf
    *.conf        text eol=lf
    
    *.awk  text eol=lf
    *.sed  text eol=lf
    *.sh   text eol=lf
    
    *.png  binary
    *.jpg  binary
    
    *.p12  binary
    

    It's a bit more things to set up, but do it once per project, and any contributor on any OS should have no troubles with line endings when working with this project.

提交回复
热议问题