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で改行コードの自動変換を無効化する方法

Posted at

はじめに

Gitは環境によって改行コード(LF/CRLF)を自動的に変換します。
この機能は便利な一方で、環境差による差分やビルドエラーなど、思わぬトラブルの原因になることもあります。

そこで本記事では、改行コードの自動変換を無効化する設定方法を紹介します。

1. git config で設定する

改行コードの自動変換によるトラブルを避けたい場合、
以下の2つの設定を行うのが基本です。

git config --global core.autocrlf false
git config --global core.safecrlf true
  • core.autocrlf : リポジトリ内の改行コードを自動変換する設定。false にすると、変換を行いません。
  • core.safecrlf : 改行コードが混在しているファイルのコミットを制御する設定。true にすると、混在がある場合はコミットを拒否します。

もし開発環境をチームで統一できる場合は、上記コマンドをGitインストール時の初期設定として手順書に含めると良いでしょう。

2. .gitattributes でリポジトリ単位に制御する

グローバル設定のほかに、.gitattributes ファイルを使ってリポジトリごとに改行コードを統一することも可能です。

# 改行コードを LF に統一
* text eol=lf

# Windows 向けバッチファイルだけ CRLF を維持
*.bat text eol=crlf

このように .gitattributes を設定しておくと、プロジェクトごとに改行コードの方針を明確に保てます。

環境

  • git version 2.43.0.windows.1
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?