Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Git: LF will be replaced by CRLF the next time Git touches it

The warning describes the next checkout, not the commit. core.autocrlf and .gitattributes decide it; scripts need eol=lf or they break on Linux.

~5 min readGit
#Git#Line Endings#Windows#Configuration
A Git file crate with two line-ending rails, a cyan normalization gate and an amber attributes card

Introduction & Quick Summary

text
warning: in the working copy of 'scripts/deploy.sh', LF will be replaced by CRLF the next time Git touches it

Git is telling you that its line-ending policy will rewrite the file at the next checkout, not that the file is broken. The warning appears during git add when the working tree contains one ending style and the index or checkout policy expects another.

Two settings decide the behavior, and they are frequently confused:

  • core.autocrlf — a per-machine preference: true converts on commit and checkout (Windows default), input converts only on commit (recommended for macOS/Linux), false stores bytes as-is.
  • .gitattributes text / eol — a per-repository rule that overrides the machine preference and travels with the code.

The warning itself is harmless. The bug it predicts is not: a shell script committed with CRLF fails on Linux with /usr/bin/env: 'bash\r': No such file or directory, and a .bat file normalized to LF breaks cmd.exe.

core.autocrlfOn commitOn checkoutTypical host
trueCRLF to LFLF to CRLFWindows default
inputCRLF to LFNo conversionmacOS, Linux
falseNo conversionNo conversionServers, CI
unsetNo conversionNo conversionGit default

Core Mechanics, Signatures & Reproducible Setup

Git stores blobs in the index. For paths marked as text, git add normalizes line endings to LF in the index, and git checkout materializes them according to the checkout rule. The warning is emitted when normalization is about to change the working tree content in a way the user did not explicitly request.

Reproduce it on Linux by forcing an autocrlf-style conversion:

bash
mkdir crlf-demo && cd crlf-demo git init -q printf 'echo hello\n' > deploy.sh git config core.autocrlf true git add deploy.sh # warning: in the working copy of 'deploy.sh', LF will be replaced by CRLF # the next time Git touches it

The blob in the index now contains LF, and the next git checkout -- deploy.sh writes CRLF into the working tree. Inspect what is actually stored:

bash
git ls-files --eol # i/lf w/crlf attr/ deploy.sh # ^index ^worktree

i/ is the index, w/ is the working tree, and attr/ shows the attribute that applied. This single command answers most line-ending questions faster than any config dump.

The repository-level fix is a .gitattributes file at the root:

gitattributes
* text=auto *.sh text eol=lf *.py text eol=lf *.bat text eol=crlf *.cmd text eol=crlf *.png binary *.jpg binary *.pdf binary

text=auto lets Git detect text files and normalize them; explicit eol overrides the platform-dependent checkout for files whose consumer requires a specific ending. binary is shorthand for -text -diff, which keeps the bytes untouched.

After adding the file, renormalize the existing history's working tree once:

bash
git add --renormalize . git status git commit -m "Normalize line endings"

--renormalize re-adds tracked files so the index is rewritten according to the new attributes. It does not rewrite history; older commits keep their bytes, and only the new commit changes them.

Key Comparison Table

SettingWhere it livesOverrides the otherGood for
core.autocrlf=trueMachine configLoses to eol= attributesWindows-only teams
core.autocrlf=inputMachine configLoses to eol= attributesMixed-OS teams
text=auto.gitattributesBeats autocrlfRepositories
eol=lf.gitattributesBeats everythingShell scripts, Dockerfiles
eol=crlf.gitattributesBeats everythingBatch files
-text / binary.gitattributesDisables all conversionImages, archives
SymptomCauseFix
Warning on git addWorking tree differs from policyExpected; commit or renormalize
Script fails with \rFile committed with CRLFeol=lf plus renormalize
Whole file shows as changedAutocrlf changed on one machineRenormalize, then commit once
Warning persists after commitPolicy converts back on checkoutSet eol=lf for that path
Binary file corruptedText detection on imageMark binary

Practical Edge Cases, Error Fixes & Deep Dive Code Examples

Case 1: The warning is not an error, but the script really breaks

bash
bash scripts/deploy.sh # /usr/bin/env: 'bash\r': No such file or directory

Check the actual bytes before changing any configuration:

bash
file scripts/deploy.sh # scripts/deploy.sh: Bourne-Again shell script, ASCII text, with CRLF line terminators

Then fix the policy for that path and renormalize:

bash
printf '*.sh text eol=lf\n' >> .gitattributes git add --renormalize scripts/deploy.sh git commit -m "Force LF for shell scripts"

dos2unix or sed -i 's/\r$//' fixes the working copy but not the next checkout, so it is a temporary measure on a machine without write access to .gitattributes.

Case 2: A repository that already contains CRLF blobs

bash
$ git ls-files --eol | grep crlf i/crlf w/crlf attr/ src/legacy.bat i/mixed w/mixed attr/ src/config.ini

An i/crlf entry means the index itself stores CRLF, so no checkout policy will normalize it. Adding text=auto and renormalizing rewrites every such file in one commit; review the diff size before pushing, because reviewers on GitHub will see entire files change.

bash
git add --renormalize . git diff --cached --stat | tail -3 # 142 files changed, 142 insertions(+), 142 deletions(-)

Case 3: safecrlf turns the warning into a failure

bash
git config --global core.safecrlf true git add data.csv # fatal: LF would be replaced by CRLF in data.csv

safecrlf=true refuses any irreversible conversion. safecrlf=warn keeps the warning without failing. Use true on a team that keeps losing scripts, and mark the genuine exceptions with -text so they are not blocked.

Case 4: Docker and CI builds see different endings

A Dockerfile or entrypoint script committed from Windows with CRLF fails at container start:

text
exec /usr/local/bin/entrypoint.sh: no such file or directory

The error is confusing because the file exists. The interpreter path is what does not exist, since the kernel reads #!/bin/sh\r as an interpreter name. Mark scripts in .gitattributes, and add a build-time guard for repositories with many contributors:

dockerfile
COPY . /app RUN sed -i 's/\r$//' /app/entrypoint.sh && chmod +x /app/entrypoint.sh

The sed is a safety net, not a substitute for the attribute; without it a future script reintroduces the bug.

Case 5: Editors rewriting endings silently

VS Code, JetBrains and Notepad++ each have their own files.eol or line separator setting. A file saved as CRLF on a machine with autocrlf=input produces the opposite warning (CRLF will be replaced by LF) on every save, and one file can flip back and forth between developers.

Pin the editor to the repository convention rather than the host OS:

{ "files.eol": "\n", "files.insertFinalNewline": true }

Then confirm with git ls-files --eol after a save; if w/ still shows crlf, the editor setting did not apply to that file type.

Case 6: Global config versus repository config

bash
git config --show-origin --get core.autocrlf # file:/home/dev/.gitconfig input

--show-origin reveals whether the value comes from the system, global, or local config. A repository that sets core.autocrlf=true locally overrides the machine default for everyone who clones it only if the setting is written to .git/config, which does not travel; use .gitattributes for anything the team must share.

Common Compilation/Runtime Mistakes & Version Gotchas

  • Assuming the warning means data loss. The commit is normalized as intended; the warning describes the next checkout. Committing and verifying with git ls-files --eol is the correct response.
  • core.autocrlf on Linux servers. Setting it to true on a Linux build host converts LF to CRLF on checkout, which breaks scripts that were fine in the repository. Use input or false outside Windows.
  • Git for Windows defaults. The installer selects true by default, so a Windows developer sees these warnings on repositories with no .gitattributes. Changing the machine setting does not fix repositories that already store CRLF blobs.
  • .gitattributes placement. Only the root file applies repository-wide; a nested .gitattributes applies to its directory subtree, and a file added before the attribute still needs renormalization.
  • text=auto and .bat files. Automatic detection sees batch files as text and converts them to LF in the index; without eol=crlf for *.bat, a Windows checkout may still produce a file cmd.exe misparses.
  • --renormalize with uncommitted work. It stages all tracked files, so run it on a clean tree or stash first. Otherwise an unrelated change is buried in a thousand-line line-ending commit.

When a diff looks like the whole file changed, the diff tool shows whether the only difference is \r. To confirm two checkouts are byte-identical after normalization, compare their hashes with the hash generator.

Primary references: gitattributes defines text, eol and binary with their precedence, and Git configuration documents core.autocrlf and core.safecrlf.

Official references

primary documentation

The Git documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit Git work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Git guides

More in Git

All Git guides

The guides filed next to this one in Git, in library order.