Skip to content
vast-cow's blog
Go back

How to Standardize Line Endings in Git Without Committing `.gitattributes`

Edit page

When developing with Git, differences between CRLF and LF can become an issue when working across Windows and Linux/macOS.

A common solution is to place a .gitattributes file in the repository to define line-ending rules.

However, there are also cases where you may want to:

Git allows you to configure equivalent settings without committing .gitattributes to the repository.

Configure Only a Specific Repository

If you want the settings to apply only to a specific repository, you can use the following file:

.git/info/attributes

Because this file is located inside the .git directory, it is normally not tracked by Git.

For example, you can configure it as follows:

* text=auto eol=lf
*.bat text eol=crlf
*.cmd text eol=crlf

With this configuration, text files are generally treated as LF, while Windows batch files use CRLF.

Here is what each setting means:

* text=auto eol=lf

For files recognized as text, this sets the line endings in the working tree to LF.

Meanwhile,

*.bat text eol=crlf
*.cmd text eol=crlf

treats .bat and .cmd files as text files and uses CRLF for them in the working tree.

In other words, the basic policy is:

Regular text files → LF
Windows batch files → CRLF

Configure Common Settings for All Repositories

If creating .git/info/attributes every time is inconvenient, you can use Git’s global configuration.

First, configure the location of the attributes file:

git config --global core.attributesFile ~/.gitattributes

Then create ~/.gitattributes:

* text=auto eol=lf
*.bat text eol=crlf
*.cmd text eol=crlf

This allows you to apply a common attributes configuration to Git repositories used in your environment.

Choosing Between .git/info/attributes and Global Configuration

It is useful to choose the appropriate method based on your needs.

MethodScopeGit-tracked
.gitattributesRepositoryYes
.git/info/attributesSpecific repositoryNo
core.attributesFileYour entire Git environmentNot tracked by individual repositories

If the entire team needs to use the same rules, placing .gitattributes in the repository is the appropriate approach.

On the other hand, for personal settings such as “I want LF to be the default only in my development environment,” it is convenient to configure:

git config --global core.attributesFile ~/.gitattributes

If you need exceptional rules for only a specific repository, you can use:

.git/info/attributes

Summary

Even if you cannot add .gitattributes to the repository, you can still use Git’s attributes mechanism.

For settings shared across all repositories, configure:

git config --global core.attributesFile ~/.gitattributes

and set:

* text=auto eol=lf
*.bat text eol=crlf
*.cmd text eol=crlf

For only a specific repository, put the same content in:

.git/info/attributes

This allows you to apply the rule LF by default, with CRLF only for Windows batch files without committing a configuration file to the repository.


Edit page
Share this post:

Comments


Previous Post
Streaming Video Over a Network with the Lowest Possible Latency Using FFmpeg + MediaMTX
Next Post
How to Use a Tool to Remove a Specific App from Windows “File Associations”