Developer Workflow

Convert CRLF to LF for Git and scripts

Detect mixed line endings and normalize CRLF to LF before committing code, running shell scripts, editing configs, or importing logs across platforms.

Problem

Windows CRLF and Unix LF line endings are both valid, but mixing them in code, scripts, configs, and logs creates noisy diffs and can break Linux execution. A shell script saved with CRLF may show /bin/bash^M errors even though the text looks normal.

When to use this

  • Git shows a large diff even though the visible text barely changed.
  • A shell script, Docker entrypoint, or CI script fails after being edited on Windows.
  • A config, log, or copied text file has mixed CRLF and LF endings before import or automation.

Steps

  1. Step 1

    Detect the current line ending mix

    Start with the line ending detector to count CRLF, LF, and CR endings. Mixed counts usually explain noisy diffs and cross-platform failures.

  2. Step 2

    Choose the target ending

    Use LF for shell scripts, Linux/macOS tooling, Docker files, and most repositories. Use CRLF only when a Windows-specific target explicitly requires it.

  3. Step 3

    Convert CRLF to LF

    Run the CRLF/LF converter and normalize the whole file to the chosen style instead of replacing only the first visible issue.

  4. Step 4

    Check BOM if the first line still fails

    If a script still fails at the first line after LF conversion, inspect and remove a BOM because BOM and CRLF often appear together in Windows-created files.

  5. Step 5

    Commit with a repository policy

    After conversion, add or verify .gitattributes and editor settings so future commits keep the same line ending policy.

Example

Fix a Windows-edited shell script

Input

#!/bin/bash\r\necho "deploy"\r\n
Error: /bin/bash^M: bad interpreter

Output

#!/bin/bash\necho "deploy"\n
Action: convert CRLF to LF, then commit the normalized script.

Common mistakes

Replacing visible blank lines instead of line endings

CRLF and LF are invisible control characters. Use detection and conversion rather than manual visual editing.

Normalizing once without a repo rule

Without .gitattributes or editor settings, another machine can reintroduce CRLF and bring back noisy diffs.

FAQ

Should code repositories use LF or CRLF?

Most cross-platform repositories store LF in Git and let editors handle local preferences. Use .gitattributes to make the policy explicit.

Why does /bin/bash^M happen?

The script likely has CRLF line endings, so Linux treats the carriage return as part of the interpreter path on the shebang line.

Can I convert only one file?

Yes. Convert the affected file first, then decide whether the repository needs a broader normalization pass.