C#と.NETプロジェクトの.gitattributes

Visual Studioソリューションファイル、NuGetパッケージ、MSBuildファイルを含むC#と.NETプロジェクト向けの完全な.gitattributes設定。

Language-Specific

詳細な説明

C# / .NETの.gitattributes

.NETプロジェクトは、Visual StudioとWindows上のMSBuildツールが特定のファイルタイプにCRLF行末を期待し、.NET Core / .NET 5+のクロスプラットフォームツーリングはLFでより良く動作するため、行末に関する特定の要件があります。

推奨設定

# 自動検出
* text=auto

# C#ソース
*.cs     text diff=csharp
*.cshtml text diff=html
*.razor  text diff=html
*.csx    text diff=csharp

# Visual Studio(CRLFが必要)
*.sln        text eol=crlf
*.csproj     text
*.vbproj     text
*.fsproj     text
*.dbproj     text
*.props      text
*.targets    text
*.ruleset    text
*.vsixmanifest text
*.vsct       text

# XML設定
*.config     text
*.xml        text
*.xaml       text
*.resx       text
*.nuspec     text

# スクリプトファイル
*.ps1   text eol=crlf
*.psm1  text eol=crlf
*.psd1  text eol=crlf
*.bat   text eol=crlf
*.cmd   text eol=crlf
*.sh    text eol=lf

# データ
*.json  text
*.csv   text
*.sql   text diff=sql

# NuGet
*.nupkg binary

# コンパイル済み
*.dll   binary
*.exe   binary
*.pdb   binary
*.cache binary

# Designerファイル(diff抑制)
*.Designer.cs text -diff
*.designer.cs text -diff

主な判断

*.sln text eol=crlf: Visual Studioのソリューションファイルパーサーはcrlf行末を要求します。これはクロスプラットフォーム.NETチームで最も一般的な問題の原因です。

*.Designer.cs text -diff: Designer生成ファイル(WinForms、EFマイグレーション)は頻繁に変更されますが、自動生成されます。diffを抑制することでPRをクリーンに保ちます。

*.ps1 text eol=crlf: PowerShellスクリプトはWindows中心であり、CRLF行末を期待します。これはLFを必要とするbashスクリプトとは異なります。

.csprojeol=crlfなし: モダンな.NET SDKスタイルのプロジェクトファイルはLFでもCRLFでも動作します。.slnのみが厳密にCRLFを要求します。

Entity Frameworkマイグレーション

EFマイグレーションは大きなdiffを生成するdesignerファイルを生成します:

Migrations/*.Designer.cs text -diff
Migrations/*ModelSnapshot.cs text -diff

ユースケース

WindowsとmacOS/Linuxの両方に開発者がいる.NETプロジェクト(ASP.NET Core、WPF、MAUI、Blazor、WinForms)は、これらの属性を設定する必要があります。.slnファイルのCRLF要件は、クロスプラットフォーム.NET開発で最も一般的な問題点です。

試してみる — .gitattributes Generator

フルツールを開く