2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

.gitattributesとは?簡単説明!

Last updated at Posted at 2025-12-13

はじめに

いろんなリポジトリを見ていたら .gitattributes というファイルを見つけました。
名前から Git 関連のファイルであることは分かりますが、使用用途はいったいなんなのでしょうか。

(関係ないですが、これは2番煎じの始まり方ですね。)

.gitattributes とは何か

調べたところ、.gitattributes は Git がファイルをどのように扱うかを指定するための設定ファイルでした。

.gitattributes には、主に改行コードの扱い、差分の取り方などの情報を記述します。

例としては以下のようになっています。

.gitattributes
* text=auto

この設定があることで、Git が自動的に改行コードを調整し、
OS の違いによる無駄な差分が発生しにくくなります。

そもそも .gitattributes とは

.gitattributes は、Git の追跡対象かどうかを決めるファイルではありません。
Git が 「追跡しているファイルを、どう扱うか」 を決めるための仕組みです。

例えば、次のような構成があったとします。

tree.txt
example-project/
├── src/
│   └── main.js
├── scripts/
│   └── build.sh
├── images/
│   └── logo.png

このとき、以下のように設定できます。

.gitattributes
*.js   text
*.sh   text eol=lf
*.png  binary

上から

  • .js.sh はテキストファイルとして扱う
  • .sh は改行コードを LF に統一する
  • .png はバイナリファイルとして扱う

といった挙動を Git に指示できます。

では、設定しないとどうなる?

.gitattributes を設定しなくても、 Git は動作します。
しかし、画像ファイルのdiffが表示され見づらくなったり、OSの違いによる改行コードの差分が大量に出るなどの問題が起こる可能性かもしれません。

そのため、特にチーム開発や OSS では .gitattributes を設定しておくことが推奨されているそうです。

.gitignore との違い

.gitignore.gitattributes は役割が異なります。

.gitignore は Git が追跡しないファイルを指定するもの。
.gitattributes は Git がどう扱うかを指定するものです。

そのため、両者は競合せず、併用されるのが一般的です。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?