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

后端 未结 9 1741
青春惊慌失措
青春惊慌失措 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:22

    Using core.autocrlf=false stopped all the files from being marked updated as soon as I checked them out in my Visual Studio 2010 project. The other two members of the development team are also using Windows systems so a mixed environment didn't come into play, yet the default settings that came with the repository always marked all files as updated immediately after cloning.

    I guess the bottom line is to find what CRLF setting works for your environment. Especially since in many other repositories on our Linux boxes setting autocrlf = true produces better results.

    20+ years later and we're still dealing with line ending disparities between OSes... sad.

    0 讨论(0)
  • 2020-11-22 08:29

    Don't convert line endings. It's not the VCS's job to interpret data -- just store and version it. Every modern text editor can read both kinds of line endings anyway.

    0 讨论(0)
  • 2020-11-22 08:33

    Two alternative strategies to get consistent about line-endings in mixed environments (Microsoft + Linux + Mac):

    A. Global per All Repositories Setup

    1) Convert all to one format

    find . -type f -not -path "./.git/*" -exec dos2unix {} \;
    git commit -a -m 'dos2unix conversion'
    

    2) Set core.autocrlf to input on Linux/UNIX or true on MS Windows (repository or global)

    git config --global core.autocrlf input
    

    3) [ Optional ] set core.safecrlf to true (to stop) or warn (to sing:) to add extra guard comparing if the reversed newline transformation would result in the same file

    git config --global core.safecrlf true
    


    B. Or per Repository Setup

    1) Convert all to one format

    find . -type f -not -path "./.git/*" -exec dos2unix {} \;
    git commit -a -m 'dos2unix conversion'
    

    2) add .gitattributes file to your repository

    echo "* text=auto" > .gitattributes
    git add .gitattributes
    git commit -m 'adding .gitattributes for unified line-ending'
    

    Don't worry about your binary files - Git should be smart enough about them.


    More about safecrlf/autocrlf variables

    0 讨论(0)
提交回复
热议问题