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
.gitattributesto 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
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.
| 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
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.
Gitで .gitattributes をコミットせずに改行コードを統一する方法
Gitで開発していると、WindowsとLinux/macOSの間で CRLFとLFの違いが問題になることがあります。
一般的な解決方法は、リポジトリに .gitattributes を配置して改行コードのルールを定義することです。
しかし、
- リポジトリには
.gitattributesを追加したくない - 自分の環境だけ改行コードを統一したい
- 複数のリポジトリで同じルールを適用したい
といったケースもあります。
Gitでは、.gitattributes をリポジトリにコミットしなくても、同等の設定を行うことができます。
特定のリポジトリだけ設定する
特定のリポジトリにだけ適用したい場合は、次のファイルを使用できます。
.git/info/attributes
このファイルは .git ディレクトリ内にあるため、通常はGitの管理対象になりません。
例えば、次のように設定します。
* text=auto eol=lf
*.bat text eol=crlf
*.cmd text eol=crlf
この設定では、基本的にテキストファイルをLFとして扱い、WindowsのバッチファイルだけCRLFにします。
それぞれの意味は次のとおりです。
* text=auto eol=lf
テキストとして認識されたファイルについて、ワーキングツリー上の改行コードをLFにします。
一方、
*.bat text eol=crlf
*.cmd text eol=crlf
では、.bat と .cmd をテキストファイルとして扱い、ワーキングツリー上ではCRLFにします。
つまり、基本方針としては、
通常のテキストファイル → LF
Windowsバッチファイル → CRLF
という構成になります。
すべてのリポジトリで共通設定する
毎回 .git/info/attributes を作るのが面倒な場合は、Gitのグローバル設定を利用できます。
まず、attributesファイルの場所を設定します。
git config --global core.attributesFile ~/.gitattributes
続いて ~/.gitattributes を作成します。
* text=auto eol=lf
*.bat text eol=crlf
*.cmd text eol=crlf
これで、自分の環境で扱うGitリポジトリに共通のattributes設定を適用できます。
.git/info/attributes とグローバル設定の使い分け
用途によって使い分けると分かりやすいです。
| 方法 | 適用範囲 | Git管理 |
|---|---|---|
.gitattributes |
リポジトリ | される |
.git/info/attributes |
特定のリポジトリ | されない |
core.attributesFile |
自分のGit環境全体 | 各リポジトリからはされない |
チーム全体で同じルールを適用する必要があるなら、リポジトリに .gitattributes を置く方法が適しています。
一方、「自分の開発環境だけLFを基本にしたい」といった個人的な設定なら、
git config --global core.attributesFile ~/.gitattributes
を設定しておくと便利です。
特定のリポジトリだけ例外的なルールが必要なら、
.git/info/attributes
を利用できます。
まとめ
リポジトリに .gitattributes を追加できない場合でも、Git attributesの仕組み自体は利用できます。
全リポジトリ共通なら、
git config --global core.attributesFile ~/.gitattributes
として、
* text=auto eol=lf
*.bat text eol=crlf
*.cmd text eol=crlf
を設定します。
特定のリポジトリだけなら、同じ内容を、
.git/info/attributes
に記述します。
これにより、基本はLF、WindowsのバッチファイルだけCRLFというルールを、リポジトリに設定ファイルをコミットすることなく適用できます。
Top comments (0)