Why should I use core.autocrlf=true in Git?

前端 未结 4 740
悲哀的现实
悲哀的现实 2020-11-22 04:40

I have a Git repository that is accessed from both Windows and OS X, and that I know already contains some files with CRLF line-endings. As far as I can tell, there are two

4条回答
  •  清歌不尽
    2020-11-22 05:11

    Update 2:

    Xcode 9 appears to have a "feature" where it will ignore the file's current line endings, and instead just use your default line-ending setting when inserting lines into a file, resulting in files with mixed line endings.

    I'm pretty sure this bug didn't exist in Xcode 7; not sure about Xcode 8. The good news is that it appears to be fixed in Xcode 10.

    For the time it existed, this bug caused a small amount of hilarity in the codebase I refer to in the question (which to this day uses autocrlf=false), and led to many "EOL" commit messages and eventually to my writing a git pre-commit hook to check for/prevent introducing mixed line endings.

    Update:

    Note: As noted by VonC, starting from Git 2.8, merge markers will not introduce Unix-style line-endings to a Windows-style file.

    Original:

    One little hiccup that I've noticed with this setup is that when there are merge conflicts, the lines git adds to mark up the differences do not have Windows line-endings, even when the rest of the file does, and you can end up with a file with mixed line endings, e.g.:

    // Some code
    <<<<<<< Updated upstream
    // Change A
    =======
    // Change B
    >>>>>>> Stashed changes
    // More code
    

    This doesn't cause us any problems (I imagine any tool that can handle both types of line-endings will also deal sensible with mixed line-endings--certainly all the ones we use do), but it's something to be aware of.

    The other thing* we've found, is that when using git diff to view changes to a file that has Windows line-endings, lines that have been added display their carriage returns, thus:

        // Not changed
    
    +   // New line added in^M
    +^M
        // Not changed
        // Not changed
    

    * It doesn't really merit the term: "issue".

提交回复
热议问题