Solving inconsistent line endings within a Git repository

Windows uses CRLF (\r\n) lined endings while Linux uses LF (\n). If like me, you develop on both Windows and Linux machines you might experience some annoying problems.

In my case, these problems consisted of “empty” commits in which many files only had changes in line endings. Mostly, these were caused by running a code style fixer that introduced these inconsistencies.

This can be fixed by adding 2 configuration files to your project. The benefit of this approach is that the fixes are easy to share among team members and consistent across different systems without requiring reconfiguration.

.gitattributes

The .gitattributes file allows one to configure “attributes” for “paths” in Git. A path is usually a file extension. Some examples of attributes include:

  • type of file (text or binary)
  • type of line ending (“lf” or “crlf”)
  • tab width
  • how to display the diff

So, start by adding .gitattributes to the root of your project. Then, copy the .gitattributes gist that we use for all our projects at SWIS.

What this does is tell Git to only use LF line endings in all files where that is appropriate. So, enable it for .css or .php but not for .png of .woff.

.editorconfig

Now that Git knows what type of line endings we want, it is time to configure our editor. The .editorconfig configuration file is meant to do just that. It has a simple markup and can be re-used by any editor.

Add a .editorconfig file to the root of your project and add an EditorConfig plugin to your editor for choice.

At SWIS, we also made a .editorconfig gist that can get you up-and-running quickly.

Cleaning things up

Now that we have successfully configured our project, it is time to clean up all incorrect line endings that are left.

My editor of choice is PHPStorm and one of the reasons is that fixing things across files is really easy. To fix all inconsistent line endings, do the following:

  • Hit shift 2 times (this will bring up the “search everywhere” dialog).
  • Search for “run inspection by name”.
  • Find the inspection “Inconsistent line separators” and run in on the whole project.
  • A list with all incorrect line separators will be displayed and there is a button to fix all issues at once.

If you don’t use PHPStorm, GitHub has a great write-up of how to fix all line endings by just using the git command line.

Once this is done, commit your changes and everything should be solved!

Further reading