DEV Community

vast cow
vast cow

Posted on

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

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:

  • Avoid adding .gitattributes to the repository
  • Standardize line endings only in your own environment
  • Apply the same rules across multiple repositories

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Then create ~/.gitattributes:

* text=auto eol=lf
*.bat text eol=crlf
*.cmd text eol=crlf
Enter fullscreen mode Exit fullscreen mode

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.

Method Scope Git-tracked
.gitattributes Repository Yes
.git/info/attributes Specific repository No
core.attributesFile Your entire Git environment Not 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
Enter fullscreen mode Exit fullscreen mode

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

.git/info/attributes
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

and set:

* text=auto eol=lf
*.bat text eol=crlf
*.cmd text eol=crlf
Enter fullscreen mode Exit fullscreen mode

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

.git/info/attributes
Enter fullscreen mode Exit fullscreen mode

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.

Gitで .gitattributes をコミットせずに改行コードを統一する方法

Gitで開発していると、WindowsとLinux/macOSの間で CRLFとLFの違いが問題になることがあります。

一般的な解決方法は、リポジトリに .gitattributes を配置して改行コードのルールを定義することです。

しかし、

  • リポジトリには .gitattributes を追加したくない
  • 自分の環境だけ改行コードを統一したい
  • 複数のリポジトリで同じルールを適用したい

といったケースもあります。

Gitでは、.gitattributes をリポジトリにコミットしなくても、同等の設定を行うことができます。

特定のリポジトリだけ設定する

特定のリポジトリにだけ適用したい場合は、次のファイルを使用できます。

.git/info/attributes
Enter fullscreen mode Exit fullscreen mode

このファイルは .git ディレクトリ内にあるため、通常はGitの管理対象になりません。

例えば、次のように設定します。

* text=auto eol=lf
*.bat text eol=crlf
*.cmd text eol=crlf
Enter fullscreen mode Exit fullscreen mode

この設定では、基本的にテキストファイルをLFとして扱い、WindowsのバッチファイルだけCRLFにします。

それぞれの意味は次のとおりです。

* text=auto eol=lf
Enter fullscreen mode Exit fullscreen mode

テキストとして認識されたファイルについて、ワーキングツリー上の改行コードをLFにします。

一方、

*.bat text eol=crlf
*.cmd text eol=crlf
Enter fullscreen mode Exit fullscreen mode

では、.bat.cmd をテキストファイルとして扱い、ワーキングツリー上ではCRLFにします。

つまり、基本方針としては、

通常のテキストファイル → LF
Windowsバッチファイル → CRLF
Enter fullscreen mode Exit fullscreen mode

という構成になります。

すべてのリポジトリで共通設定する

毎回 .git/info/attributes を作るのが面倒な場合は、Gitのグローバル設定を利用できます。

まず、attributesファイルの場所を設定します。

git config --global core.attributesFile ~/.gitattributes
Enter fullscreen mode Exit fullscreen mode

続いて ~/.gitattributes を作成します。

* text=auto eol=lf
*.bat text eol=crlf
*.cmd text eol=crlf
Enter fullscreen mode Exit fullscreen mode

これで、自分の環境で扱うGitリポジトリに共通のattributes設定を適用できます。

.git/info/attributes とグローバル設定の使い分け

用途によって使い分けると分かりやすいです。

方法 適用範囲 Git管理
.gitattributes リポジトリ される
.git/info/attributes 特定のリポジトリ されない
core.attributesFile 自分のGit環境全体 各リポジトリからはされない

チーム全体で同じルールを適用する必要があるなら、リポジトリに .gitattributes を置く方法が適しています。

一方、「自分の開発環境だけLFを基本にしたい」といった個人的な設定なら、

git config --global core.attributesFile ~/.gitattributes
Enter fullscreen mode Exit fullscreen mode

を設定しておくと便利です。

特定のリポジトリだけ例外的なルールが必要なら、

.git/info/attributes
Enter fullscreen mode Exit fullscreen mode

を利用できます。

まとめ

リポジトリに .gitattributes を追加できない場合でも、Git attributesの仕組み自体は利用できます。

全リポジトリ共通なら、

git config --global core.attributesFile ~/.gitattributes
Enter fullscreen mode Exit fullscreen mode

として、

* text=auto eol=lf
*.bat text eol=crlf
*.cmd text eol=crlf
Enter fullscreen mode Exit fullscreen mode

を設定します。

特定のリポジトリだけなら、同じ内容を、

.git/info/attributes
Enter fullscreen mode Exit fullscreen mode

に記述します。

これにより、基本はLF、WindowsのバッチファイルだけCRLFというルールを、リポジトリに設定ファイルをコミットすることなく適用できます。

Top comments (0)