Force CRLF Line Endings for Windows Files
Set up .gitattributes to enforce CRLF line endings for Windows-specific files like Visual Studio solutions, batch scripts, and PowerShell files.
Detailed Explanation
Forcing CRLF with eol=crlf
Just as some files must have LF endings, certain Windows-specific files require CRLF (\r\n) endings to function correctly. Visual Studio solution files, batch scripts, and PowerShell scripts all expect CRLF.
The Directive
*.sln text eol=crlf
*.bat text eol=crlf
*.cmd text eol=crlf
*.ps1 text eol=crlf
*.props text eol=crlf
*.vcxproj text eol=crlf
Why Visual Studio Solutions Need CRLF
Visual Studio's .sln file parser is strict about line endings. When a .sln file uses LF endings, Visual Studio may:
- Fail to load the solution correctly
- Rewrite the file with CRLF, creating a dirty working tree
- Show encoding warnings
By specifying eol=crlf, Git ensures that even macOS/Linux developers checking out the repository get CRLF endings for these files, preventing Visual Studio compatibility issues.
Batch Scripts and CRLF
The Windows command interpreter (cmd.exe) requires CRLF line endings. A batch file with LF endings may produce unexpected behavior:
- Commands may not execute correctly
IFblocks may not terminate properlyFORloops may misparse filenames
A Complete Cross-Platform Configuration
# Global
* text=auto
# Force LF (Unix tools)
*.sh text eol=lf
Makefile text eol=lf
Dockerfile text eol=lf
# Force CRLF (Windows tools)
*.sln text eol=crlf
*.bat text eol=crlf
*.cmd text eol=crlf
*.ps1 text eol=crlf
This configuration respects the requirements of both Unix and Windows tooling, ensuring that platform-specific files always have the correct line endings regardless of where they are checked out.
Use Case
Repositories that contain Visual Studio solution files (.sln), MSBuild project files (.csproj, .vcxproj), or Windows batch/PowerShell scripts should force CRLF endings for those files. This is especially important in .NET and C++ projects that target Windows.