Home

Using LF instead of CRLF on Windows

Have you ever worked on a team where people use other OS along with windows(with Git as version-control system). In such case tesing or linting will give you errors about the line endings. In this post I will walk you through some settings, which can solve these problems in your codebase.

Reason for this problem

This problem arises because of the default line endings in Windows, which is CRLF. While the default line endings in macOS and all linux distros will be LF

All the below solutions are the modifications needed to be done on Windows PC, to make it compatible with other OS.

CRLF means (\r\n) and LF means (\n)

Solutions

Solution #1

The key to dealing with line endings is to make sure your configuration is committed to the repository, using .gitattributes. For most people, this is as simple as creating a file named .gitattributes at the root of your repository that contains these 2 lines:

* text=auto
*.png -text

*.png -text is optional, but I use it as a best practice With this set, Windows users will have text files converted from Windows style line endings (\r\n) to Unix style line endings (\n) when they’re added to the repository.

For most developers - in most repositories - this will resolve all the issues with line endings.

Solution #2

You can change your editor settings to support line endings as LF. The procedure to set this up vary based on your editor. In vscode it is "files.eol": "\n"

Solution #3

While installing Git, you can choose Checkout as-is/ Commit Unix-style. If you do lot of cross-platform development this will help you.

Solution #4

You can use prettier or beautify or any other code formatter for your project. I usually use prettier either as extension or a dependeny. It currently supports various files, it's worth a try. With a vscode extension, the configuration(settings.json) will be "prettier.endOfLine": "lf"

Solution #5

Many tests depend on newlines being preserved as LF. On Windows, you can ensure this by cloning with:

git -c core.autocrlf=false clone https://github.com/awsm-page/eol.git

Some other git commands that might be helpfult regarding this config:

# setting autocrlf=false for current repo
git config core.autocrlf false

# view current repo config file in editor(recommended)
git config --edit

# view current repo config in terminal
git config -l

# setting autocrlf=false globally
git config --global core.autocrlf false

# view current repo config file in editor
git config --global --edit

Explanation

It is a good idea to keep a .gitattributes file as we don't know if everyone in our team will set their config. This file should be kept in repo's root path.

* text=auto

This will treat all files as text files and convert to OS's line ending on checkout and back to LF on commit automatically. If you want to set this explicitly, then use

* text eol=crlf
* text eol=lf

First one is for checkout and second one is for commit.



Frequently asked questions

What is CRLF?

CRLF stands for Carriage Return Line Feed. It's used to note the termination of a line(EOF), however, dealt with differently in today's popular Operating Systems.

What is OS?

OS stands for Operating System. Examples of OS are Windows 10, Windows XP, Mac, Ubuntu.



Last Updated on

Next Post: GraphProtocol: TS2322 null assignment in Subgraph →

Comments