gitnewlinecommitcore.autocrlflf

How to commit files to git with line separator changed (getting 'nothing to commit')


I need to update some files for a pull request so that they have matching line separators to the rest of the project. I've changed these files from LF to CRLF in notepad++ (couldn't figure out how to do it in intellij), now I'm trying to commit this change - and nothing else.

No matter how I try to commit and push the changes - i get 'nothing to commit'. Intellij seemed to notice a difference at first since when I right click and commited the repo it selected those files for committing - then the commit failed with the same error.


Solution

  • It sounds like in this case you've turned on core.autocrlf. This is the intended and proper way to work on Windows, since it stores LF endings in your repository and converts them to CRLF if you're on Windows. However, it does mean that Git doesn't view a change of line endings as a change that can be committed, since the line endings in the repository are LF either way.

    If you are completely, absolutely certain that your codebase will never be used on a non-Windows system and will never be used by anybody using the Windows Subsystem for Linux, then you can turn that flag off, and Git won't change your line endings, so you'll store CRLF (or more accurately, whatever line endings you used) in the repository. However, if you have users using macOS, Linux, or WSL, you will make them very cross with you if you do this, and then you'll likely have dueling line ending wars in your repository.

    If instead you need your line endings for a particular set of files to always be checked out with a particular sequence (e.g., you have a batch file and CMD can't deal with LF-only endings), then you'd add an entry into your .gitattributes file that marks them appropriately:

    *.bat eol=crlf
    

    Similarly, if you have shell scripts that always must use LF endings, you can write this:

    *.sh eol=lf
    

    These aren't designed for preferences or cross-platform compatibility, but instead where the file is literally broken and can't ever work if it has the wrong line endings. If the problem is just an editor (even an editor that everyone uses) or a compiler, let folks use core.autocrlf to fix the problem on their systems.