0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

git addした際のLF will be replaced by CRLFについて

Posted at

git addすると以下のようなメッセージが出ることがある。
毎度調べていて、時間の無駄なのでちゃんと理解しようと思った。

git add .
warning: LF will be replaced by CRLF in mysql/auto.cnf.
The file will have its original line endings in your working directory

原因

gitには改行コードを統一するために、自動で改行コードを変換してくれる機能がある。
CRLF → LF
にcommitしたとき、チェックアウトしたときに変換してくれる。
gitでは改行コードはLFで管理するという方針になっているため。

改行コード

そもそも何故こんなことになっているかというと、OSごとに改行の表現が違うから。
LinuxとかmacOSは改行をLFで表す。
windowsはCRLFで表す。
これがごちゃ混ぜになっていると正しく表示されないなどのトラブルにつながるので統一したい。

対策

今回の警告は主にwindowsで起きるはず。
放置しても問題はないが、以下の設定をfalseにすることで自動変換を防げる。

git config --global core.autoCRLF false

オマケ:chatgptに聞いてみた結果。

 LF will be replaced by CRLF 警告について
原因: この警告は、Gitがファイルの行末文字を変換しようとしているために発生します。これはWindows環境でよく見られます。Gitはデフォルトで、リポジトリ内ではLF(Line Feed)を使用し、チェックアウト時にCRLF(Carriage Return + Line Feed)に変換します。
解決方法:
警告を無視しても問題ありませんが、行末文字の変換を避けたい場合、次のコマンドで変換を無効化できます:
bash
コードをコピーする
git config core.autocrlf false
すでに変換されたファイルを修正する場合は、次のコマンドを実行します:
bash
コードをコピーする
git rm --cached -r .
git reset --hard
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?