💻 syscmds.net

Prevent git EOL mangling

By default git will mangle text files on checkout and modify the line endings to match the default expected by your operating system. This means crlf on Windows and lf on Linux.

This can result in very weird behaviour. In one project I discovered some tests that changed the expected line endings in some test data depending on the OS the tests were running under. Unfortunately these weren't very good tests since the input format (mbox) required line endings of a specific type!
In another situation, a git clone was run under Windows and then a bash script was run in WSL (Windows Subsystem for Linux). The Windows line endings meant that the arguments split over multiple lines were treated as separate, and invalid, commands.

Changing git EOL defaults

I recommend switching off this mangling globally for your user account. This can be done with the following command:

$ git config --global core.autocrlf true

If you're using git for windows, there is also an option to do this as part of the installer.

After this has been set any files already checked out will remain the same. Therefore it is advisable to checkout a fresh copy of your projects.

Setting per-project EOL options

You can't rely on every developer having set these default options. Therefore it is probably also necessary to set this on a per project basis. This can be done by using adding (or amending) a .gitattributes file in the project root. This example disables text mangling for all file types.

* -text

After this has been committed any files already checked out will remain the same. Therefore it is advisable to checkout a fresh copy of your projects. Git allows you to view the attributes and confirm that these have been applied:

$ git check-attr --all .
.: text: unset

Additionally, view the attributes on specific files:

$ git ls-files --eol ./gradle.properties
i/lf    w/lf    attr/-text            gradle.properties

The first column shows the line endings in the index, the next the endings in the working tree. The third column shows that the attribute has been correctly applied to this file.

More Information

For in depth information see the relevant git documentation on gitattributes, config, check-attr and ls-files.

Last Modified:
Tags: eol, git