accepted
on 2023-11-27

Use .gitattributes to fix line endings

Context and Problem Statement

Windows machines and unix systems use different line endings. Modern editors, for instance Visual Studio Code, can handle any line endings. For consistency, I want to us unix line endings (LF) everywhere. This can be enforced by prettier as selected by ADR-0006.

Considered Options

  • Git autocrlf
  • .gitattributes file

Decision Outcome

Chosen option: “.gitattributes file”, because it applies regardless of user settings.

Pros and Cons of the Options

Git autocrlf

Set core.autocrlf to true on my (windows) developer machine.

This will Git to ensure line endings in files you checkout are correct for Windows. For compatibility, line endings are converted to Unix style when you commit files.

  • Good, because it is simple to configure
  • Good, because it requires no maintenance
  • Neutral, because it requires CI to check line endings on GitHub
  • Bad, because it has to be done on every machine I use for development

.gitattributes file

Configure a .gitattributes file to manage how Git reads line endings.

  • Good, because it ensures consistent behavior for all users, regardless of their Git settings and environment
  • Neutral, because it requires CI to check line endings on GitHub
  • Bad, because new file types need to be added to the file whenever they’re added to the project

More Information

See GitHub’s Getting started with Git.

The .gitattributes and check-gitattributes.sh files are sourced from .gitattributes Templates.