In this article, we analyse the .gitattributes from the react/.gitattributes. When you are part of a team, you and your colleagues often work on projects across different operating systems and development environments. As a result, file formatting issues, especially related to line endings, can arise.
Linux/macOS use a Line Feed (LF) character.
Windows uses a Carriage Return + Line Feed (CRLF) combination.
using .gitattributes
you can define file’s line endings. We will study the .gitattributes from React source and find out how.
What is .gitattributes
?
.gitattributes
is a configuration file that defines attributes for paths in a Git repository. These attributes allow you to control how Git processes different types of files during specific operations, such as:
Check-in and check-out: How files are stored in the repository and retrieved in the working directory. Link to docs
Diff and merge behavior: How changes to files are compared or merged. Link to docs.
EOL (End of Line) conversion: Ensuring line endings are consistent across operating systems (Linux/macOS vs. Windows). Link to docs
The file can be committed into the repository and versioned, meaning its rules are applied consistently for every collaborator, regardless of their local settings.
Why is .gitattributes
Important?
Cross-Platform Consistency: Different operating systems use different end-of-line (EOL) characters (LF on Unix-based systems, CRLF on Windows). Without
.gitattributes
, inconsistencies in line endings can result in unnecessary changes being detected, cluttering your Git history and causing conflicts.Binary vs. Text File Handling: Git automatically tries to detect whether a file is binary or text, but it may not always get it right (e.g., files that are partially ASCII but contain binary data).
.gitattributes
ensures that files are treated correctly, whether they’re text, binary, or specific formats like images or PDFs.
You can read this more at this in-depth Stackoverflow answer.
3. Control Over Diffs and Merges: .gitattributes
can define custom diff and merge strategies, enabling better control over how files are compared and resolved during conflict situations.
Common .gitattributes
Configuration
A typical .gitattributes
file looks something like this:
# Auto-detect text files and ensure LF line endings in the repository
* text=auto
# Windows batch scripts must use CRLF
*.bat text eol=crlf
# Go files should use LF only
*.go text eol=lf
# Binary files should not be modified by Git
*.png binary
*.jpg binary
*.pdf binary
text=auto
: Automatically detects whether a file is text or binary. If the file is text, Git converts line endings to LF on check-in. This prevents cross-platform issues caused by different EOL characters.*.bat text eol=crlf
: Forces Windows batch scripts to always use CRLF for proper execution on Windows.*.go text eol=lf
: Ensures Go files use LF, which is the standard line ending on Linux and macOS.*.png binary
: Prevents Git from attempting to diff or alter binary files like PNGs, which are not human-readable and should not have EOL conversions.
.gitattributes
in React
React uses a minimal .gitattributes
file:
* text=auto
This simple line ensures that files with text content (such as JavaScript files) have their line endings converted to LF in the repository while preserving the developers’ local EOL preferences. This setup is enough to prevent line-ending issues across platforms while keeping the repository clean and consistent.
Key Attributes Explained
1. text
:
This attribute enables Git’s automatic handling of line-ending normalization. It ensures that line endings are consistent (LF) in the repository while allowing the developer’s system to handle them according to local conventions (e.g., CRLF on Windows).
Set: Enables EOL conversion on check-in and checkout, normalizing line endings to LF in the repository.
Unset: Disables any line-ending conversion for the specified files.
text=auto
: Automatically detects if a file is text or binary. Git performs EOL normalization if the file is identified as text.
Is .gitattributes
Really Necessary?
The necessity of .gitattributes
depends on the project. For simple projects, Git is often smart enough to handle file types and line endings automatically. However, in larger projects with cross-platform teams, not having a .gitattributes
file can lead to:
- Inconsistent Line Endings: Developers on Windows may accidentally commit CRLF line endings, creating unnecessary changes in the repository.
Imagine just the EOL lines changes appearing in your pull request?
Incorrect Binary Handling: Git may misinterpret binary files as text, leading to corrupt diffs or merge conflicts.
Merge and Diff Issues: Custom merge strategies (e.g., for Unity or image files) are impossible without
.gitattributes
.
StackOverflow answer:
A common concern raised on StackOverflow is whether .gitattributes
adds unnecessary complexity to a project.
“Use
.gitattributes
if and only if you have to have Git mess with files because users A through M use macOS and users N through Z use Windows.”
In short, while not strictly mandatory, .gitattributes
provides a level of control that can prevent subtle issues and improve developer experience.
React devs kept it simple by just adding 1 line * text=auto
to ensure you are not including EOL changes as part of your pull request.
About us:
At Think Throo, we are on a mission to teach the advanced codebase architectural concepts used in open-source projects.
10x your coding skills by practising advanced architectural concepts in Next.js/React, learn the best practices and build production-grade projects.
We are open source — https://github.com/thinkthroo/thinkthroo (Do give us a star!)
Up skill your team with our advanced courses based on codebase architecture. Reach out to us at hello@thinkthroo.com to learn more!
Top comments (0)