til/applied-sciences/engineering/line-endings-lf-crlf
line-endings-lf-crlf.mdupdated 2026-07-162608 words
ダブルクリックで英日反転
Applied Sciences · Engineering

Line Endings: LF, CR, and CRLF

EN

Every text file contains invisible control characters that mark line breaks. Three standards exist — LF (Unix), CR (old Mac), and CRLF (Windows) — and mixing them causes real problems in team development.

The three line-ending types

  • LF (0x0A, \n) — Unix / Linux / modern macOS
  • CR (0x0D, \r) — Classic Mac OS 9 and earlier
  • CRLF (0x0D 0x0A, \r\n) — Windows
  • Names come from typewriter mechanics: CR returns print head; LF advances paper

Why mixing them causes problems

  • Git shows massive diffs even when content is identical — every line appears changed
  • Shell scripts gain a stray \r on Windows, causing syntax errors when run on a server
  • A CRLF file opened in an LF-only app may render as one long line

Git's autocrlf warning

  • Warning 'LF will be replaced by CRLF' fires when core.autocrlf = true
  • true: converts CRLF→LF on commit, LF→CRLF on checkout (Windows default)
  • input: converts CRLF→LF on commit only (Linux/Mac recommended)
  • The warning is harmless, but can produce unintended diffs across the team

Best practice: .gitattributes

  • Place a .gitattributes file at the repo root to fix line endings per repository
  • Use: * text=auto eol=lf to standardise everything to LF
  • Overrides each developer's personal core.autocrlf setting
  • Windows editors still display as CRLF internally, so practical impact is minimal
Commit a .gitattributes with eol=lf to end line-ending chaos across OS boundaries.
応用科学 · エンジニアリング

改行コード LF・CR・CRLF の​違いと​ Git 警告の​意味

JP

テキストファイルには​「行の​終わり」を​示す不可視の​制御文字が​存在する。​LF​(Unix系)​・CR​(旧Mac)​・CRLF​(Windows)の​3種が​混在すると、​チーム開発でさまざまな​不具合を​引き起こす。

3種の​改行コード

  • LF​(0x0A、​\n)​— Unix / Linux / 現代の​ macOS
  • CR​(0x0D、​\r)​— Classic Mac OS​(OS 9以前)
  • CRLF​(0x0D 0x0A、​\r\n)​— Windows
  • 名前は​タイプライターの​動作に​由来:CR=キャリッジ戻し、​LF=紙送り

混在すると​起きる​問題

  • 内容が​同一でも​ Git の​差分​(diff)が​全行変更扱いに​なる
  • Windows で​編集した​シェルスクリプトに​ \r が​残り、​サーバーで​構文エラーに​なる
  • LF 前提の​アプリで​ CRLF ファイルを​開くと​改行が​認識されず1行に​見える

Git の​警告​「LF will be replaced by CRLF」

  • core.autocrlf​(自動改行変換設定)が​ true の​時に​表示される
  • true: コミット時に​ CRLF→LF、​チェックアウト時に​ LF→CRLF へ​変換​(Windows推奨)
  • input: コミット時のみ​ CRLF→LF 変換​(Linux/Mac推奨)
  • 警告自体は​無害だが、​意図しない​差分を​生む​可能性が​ある​ため統一が​望ましい

推奨:.gitattributes で​明示的に​統一

  • .gitattributes を​リポジトリルートに​置き、​改行コードを​リポジトリ単位で​固定
  • 設定例:* text=auto eol=lf​(テキストファイルを​すべて​ LF に​統一)
  • 開発者個人の​ core.autocrlf 設定を​上書きできる
  • Windows エディタは​内部​表示を​ CRLF のままに​できるので​実害は​ほぼない
.gitattributes に​ eol=lf を​1行追加するだけで、​OS混在チームの​改行コード問題を​根絶できる。
148 notestil