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
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.
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
- Reveal animations on scroll with react-spring
- Gatsby background image example
- Extremely fast loading with Gatsby and self-hosted fonts