This Git warning appears on Windows when files have Unix-style line endings (LF) but Git is configured to convert them to Windows-style (CRLF). While not an error, this indicates a line ending configuration mismatch that can cause issues in cross-platform projects. Configure core.autocrlf appropriately or use .gitattributes for consistent handling.
The "LF will be replaced by CRLF" warning tells you that Git is automatically converting line endings in your files. This happens because: **LF (Line Feed, \n)** is the Unix/Linux/macOS line ending standard. **CRLF (Carriage Return + Line Feed, \r\n)** is the Windows line ending standard. When `core.autocrlf` is set to `true` (the default on Windows Git installations), Git performs automatic conversions: - **On checkout**: Converts LF to CRLF so files work correctly in Windows editors - **On commit**: Converts CRLF back to LF to keep LF in the repository This warning appears when you add or stage a file that has LF endings and Git is going to convert it to CRLF in your working directory. The message "The file will have its original line endings in your working directory" is somewhat confusing - it actually means the file you're committing will be stored with LF in the repository, while your local copy will be converted to CRLF. This is not an error, but a notification that automatic line ending conversion is happening. For many projects, especially those with developers on different operating systems, consistent line endings are important to avoid: - Merge conflicts caused purely by line ending differences - Diffs showing entire files as changed when only line endings differ - Build or script failures on systems expecting specific line endings
First, understand that this is a warning, not an error. Your files will still be committed correctly. The warning tells you:
1. The file currently has LF (Unix) line endings
2. Git will store it with LF in the repository (good for cross-platform)
3. Git will convert it to CRLF in your working directory (so Windows tools work)
Check your current autocrlf setting:
git config --global core.autocrlfCommon values:
- true - Converts LF to CRLF on checkout, CRLF to LF on commit (Windows default)
- input - Converts CRLF to LF on commit only, no conversion on checkout
- false - No automatic conversion
To see which files trigger the warning:
# Add files verbosely to see warnings
git add -v .The best setting depends on your team and project:
Option 1: Keep autocrlf=true (Recommended for Windows-only teams)
git config --global core.autocrlf true- Files are CRLF locally, LF in repo
- The warning is expected - you can ignore it
- Windows tools work correctly
Option 2: Use autocrlf=input (Recommended for cross-platform teams)
git config --global core.autocrlf input- Files keep their endings locally
- Only converts CRLF to LF on commit
- Avoids the warning but you may have LF files locally
Option 3: Disable autocrlf=false (Not recommended)
git config --global core.autocrlf false- No automatic conversion
- Files stay exactly as they are
- Risk: CRLF might accidentally get committed to the repository
Which should you choose?
- Windows-only project: true is fine
- Cross-platform project: Use input or .gitattributes (see next step)
- Linux/macOS: input or false (CRLF rarely appears)
The best practice for cross-platform projects is using a .gitattributes file in the repository root. This overrides individual user settings and ensures consistency.
Create a basic .gitattributes file:
# Create the file in your repository root
touch .gitattributesAdd these contents for a typical project:
# Set default behavior to automatically normalize line endings
* text=auto
# Explicitly declare text files you want to be normalized
*.c text
*.h text
*.cpp text
*.hpp text
*.py text
*.js text
*.ts text
*.json text
*.xml text
*.yml text
*.yaml text
*.md text
*.txt text
*.css text
*.html text
*.sh text eol=lf
*.bash text eol=lf
# Declare files that should always have CRLF line endings on checkout
*.bat text eol=crlf
*.cmd text eol=crlf
*.ps1 text eol=crlf
# Declare binary files that should not be modified
*.png binary
*.jpg binary
*.jpeg binary
*.gif binary
*.ico binary
*.pdf binary
*.zip binary
*.gz binary
*.tar binary
*.exe binary
*.dll binary
*.so binary
*.dylib binaryCommit the .gitattributes file:
git add .gitattributes
git commit -m "Add .gitattributes for consistent line endings"If you've added a .gitattributes file to an existing repository, you should normalize all files to apply the new settings:
Step 1: Save your work
git stashStep 2: Remove all files from the index (not from disk)
git rm --cached -r .Step 3: Re-add all files (this applies .gitattributes)
git reset --hardStep 4: Add all files back
git add .Step 5: Check what changed
git status
# You'll see files that had their line endings normalizedStep 6: Commit the normalized files
git commit -m "Normalize line endings"Step 7: Restore your stashed work
git stash popThis process rewrites files to match your .gitattributes rules.
Prevent future warnings by configuring your editor to match your project's conventions.
VS Code:
Add to your workspace or user settings (.vscode/settings.json or user settings):
{
"files.eol": "\n"
}Or use "\r\n" for CRLF if that's your standard.
Sublime Text:
Add to Preferences > Settings:
{
"default_line_ending": "unix"
}JetBrains IDEs (IntelliJ, PyCharm, etc.):
1. Go to Settings > Editor > Code Style
2. Set "Line separator" to your preferred format
3. Can also be set in .editorconfig
Create an .editorconfig file (works with most editors):
# .editorconfig
root = true
[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8
[*.{bat,cmd,ps1}]
end_of_line = crlfMost modern editors support EditorConfig natively or via plugins.
If you understand the behavior and just want to suppress the warning:
Option 1: Set safecrlf to false (suppresses the warning)
git config --global core.safecrlf falseNote: This only suppresses the warning. Line ending conversion still happens based on core.autocrlf.
Option 2: Use quiet mode for add
git add . 2>/dev/nullOption 3: Accept that warnings are informational
The warning doesn't prevent any operations. If your project works correctly, you can simply ignore it.
When NOT to suppress:
- If you're unsure about your line ending configuration
- If you're seeing related issues (weird diffs, merge conflicts)
- If team members report problems with line endings
The warning exists to help you catch configuration issues early.
If specific files have wrong line endings that you need to fix:
Convert a file from CRLF to LF:
On Linux/macOS/WSL:
# Using sed
sed -i 's/\r$//' filename.txt
# Using dos2unix (install if needed: apt install dos2unix)
dos2unix filename.txt
# Using tr
tr -d '\r' < filename.txt > filename.tmp && mv filename.tmp filename.txtOn Windows with Git Bash:
dos2unix filename.txt
# Or using sed
sed -i 's/\r$//' filename.txtOn Windows with PowerShell:
(Get-Content filename.txt -Raw) -replace "\r\n", "\n" | Set-Content filename.txt -NoNewlineConvert a file from LF to CRLF:
# Using unix2dos
unix2dos filename.txt
# Using sed
sed -i 's/$/\r/' filename.txtBulk convert all files:
# Find and convert all text files to LF
find . -type f -name "*.txt" -exec dos2unix {} \;
find . -type f -name "*.sh" -exec dos2unix {} \;Understanding Line Endings in Depth:
Historical Context:
- CR (\r, 0x0D): Carriage Return - moves cursor to start of line (old Macs)
- LF (\n, 0x0A): Line Feed - moves cursor down one line (Unix/Linux/macOS)
- CRLF (\r\n): Both characters - standard for Windows/DOS
Old typewriters needed both: CR to move the carriage back, LF to advance the paper. Windows inherited this from DOS, which came from CP/M.
The core.autocrlf Matrix:
| Setting | On checkout | On commit | Best for |
|---------|------------|-----------|----------|
| true | LF → CRLF | CRLF → LF | Windows developers |
| input | No change | CRLF → LF | Cross-platform, keep LF |
| false | No change | No change | When you manage it yourself |
The Opposite Warning:
You might also see:
warning: CRLF will be replaced by LF in file.txtThis happens when:
- autocrlf = input or autocrlf = true
- You add a file with CRLF endings
- Git will convert to LF in the repository
Why .gitattributes is Better Than core.autocrlf:
1. Repository-specific: Settings travel with the repo
2. Per-file control: Different rules for different file types
3. Team consistency: Everyone uses the same settings
4. Override user config: Works regardless of local Git settings
The text=auto Attribute:
* text=autoThis tells Git to:
1. Auto-detect whether a file is text or binary
2. Apply line ending conversion only to text files
3. Use the platform's native line endings on checkout
Forcing Line Endings for Specific Files:
# Always use LF (for shell scripts)
*.sh text eol=lf
# Always use CRLF (for Windows batch files)
*.bat text eol=crlf
# Never convert (treat as binary)
*.png binary
# Force text handling even if binary chars detected
*.svg textThe eol Attribute:
- eol=lf: Always checkout with LF
- eol=crlf: Always checkout with CRLF
- Without eol: Use native line endings based on platform
Common Pitfalls:
1. Mixed line endings in a file: Some lines CRLF, some LF. Git may not detect this well. Use tools like file or dos2unix to diagnose.
2. Binary files detected as text: Git might try to convert binary files, corrupting them. Use .gitattributes to mark them as binary.
3. Scripts failing on Unix: Shell scripts with CRLF won't run:
/bin/bash^M: bad interpreter: No such file or directoryAlways use eol=lf for shell scripts.
4. Git hooks with CRLF: Hooks in .git/hooks or committed hooks won't execute if they have Windows line endings on Unix systems.
WSL and Docker Considerations:
When using WSL (Windows Subsystem for Linux) or Docker on Windows:
- Files on Windows filesystem (/mnt/c/) may have CRLF
- Files on Linux filesystem may have LF
- Git in WSL may behave differently than Git for Windows
- Consider where your repo lives and which Git you're using
Checking File Line Endings:
# Show line ending type
file filename.txt
# Output: "filename.txt: ASCII text" (LF) or "ASCII text, with CRLF line terminators"
# Show actual bytes (look for 0d 0a = CRLF)
hexdump -C filename.txt | head
# Count line ending types in a file
cat filename.txt | od -c | grep -c '\r'Git Configuration Hierarchy:
Line ending settings can be set at multiple levels (later overrides earlier):
1. System: git config --system
2. Global: git config --global
3. Repository: git config --local
4. Worktree: git config --worktree
5. .gitattributes: Per-file overrides
Use git config --show-origin core.autocrlf to see where the setting comes from.
warning: BOM detected in file, this may cause issues
UTF-8 Byte Order Mark (BOM) detected in file
fatal: Server does not support --shallow-exclude
Server does not support --shallow-exclude
warning: filtering out blobs larger than limit
Git partial clone filtering large blobs warning
fatal: Server does not support --shallow-since
Server does not support --shallow-since in Git
kex_exchange_identification: Connection closed by remote host
Connection closed by remote host when connecting to Git server