Dealing with line endings in Windows with Git and ESLint


If you’re using a Windows machine, and you try to checkout a branch, Git may replace (depending on your configuration) your carefully placed LF line endings with CRLF (more info on CRLF and LF here). This can be frustrating if your ESLint configuration wants LF for line endings. In other words, you get a ton of linting errors every time you change a branch.

This is unacceptable. You must put an end to this madness. 😡


Luckily, the solution is pretty simple. You just add the following .gitattributes file in your project’s root. More details and additional configurations are described in this GitHub help article.

# Set the default behavior, in case people don’t have core.autocrlf set.
* text=auto

# Declare files that will always have LF line endings on checkout.
*.js text eol=lf
*.jsx text eol=lf

# Denote all files that are truly binary and should not be modified.
*.png binary
*.jpg binary

Other things you can do

If you want some extra sauce, you can also add an .editorconfig file. This tool overrides your code editor’s behavior and applies those settings whenever you save or edit a file. To use it in VS Code, you’ll also have to install the EditorConfig plugin. The following .editorconfig file is directly copied from Airbnb’s JavaScript style guide repository on GitHub:

root = true

indent_style = space
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
end_of_line = lf
# editorconfig-tools is unable to ignore longs strings or urls
max_line_length = null

A note from Captain Obvious: If you’re using a different style guide, you may have to adjust the settings.

Final words

It’s embarrassing to admit how many times I’ve typed the following commands in the terminal every time I changed a branch with Git:

yarn format
# or
yarn eslint --ext js,jsx src --fix

git add .
git status

I hope that you searched for a solution to this problem earlier than me. 👌

Other things to read