C# and .NET Project .gitattributes
A complete .gitattributes configuration for C# and .NET projects including Visual Studio solution files, NuGet packages, and MSBuild files.
Detailed Explanation
C# / .NET .gitattributes
.NET projects have specific requirements around line endings because Visual Studio and MSBuild tools on Windows expect CRLF endings for certain file types, while the .NET Core / .NET 5+ cross-platform tooling works better with LF.
Recommended Configuration
# Auto detect
* text=auto
# C# source
*.cs text diff=csharp
*.cshtml text diff=html
*.razor text diff=html
*.csx text diff=csharp
# Visual Studio (require 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
*.config text
*.xml text
*.xaml text
*.resx text
*.nuspec text
# Script files
*.ps1 text eol=crlf
*.psm1 text eol=crlf
*.psd1 text eol=crlf
*.bat text eol=crlf
*.cmd text eol=crlf
*.sh text eol=lf
# Data
*.json text
*.csv text
*.sql text diff=sql
# NuGet
*.nupkg binary
# Compiled
*.dll binary
*.exe binary
*.pdb binary
*.cache binary
# Designer files (suppress diff)
*.Designer.cs text -diff
*.designer.cs text -diff
Key Decisions
*.sln text eol=crlf: Visual Studio's solution file parser requires CRLF. This is the most common source of issues in cross-platform .NET teams.
*.Designer.cs text -diff: Designer-generated files (WinForms, EF migrations) change frequently but are auto-generated. Suppressing diffs keeps PRs clean.
*.ps1 text eol=crlf: PowerShell scripts are Windows-centric and expect CRLF endings. This differs from bash scripts which need LF.
No eol=crlf for .csproj: Modern .NET SDK-style project files work fine with either LF or CRLF. Only .sln strictly requires CRLF.
Entity Framework Migrations
EF migrations generate designer files that produce large diffs:
Migrations/*.Designer.cs text -diff
Migrations/*ModelSnapshot.cs text -diff
Use Case
Any .NET project (ASP.NET Core, WPF, MAUI, Blazor, WinForms) with developers on both Windows and macOS/Linux should configure these attributes. The CRLF requirement for .sln files is the most common pain point in cross-platform .NET development.