How do you handle End-of-Line characters in Git on Windows / Linux / Mac Operating Systems?


This question can also be rephrased as − How do you resolve the Git warning − "LF will be replaced by CRLF"?

The End−of−Line is marked using two special characters "\r
" in Windows Operating System while the “
" character is used to mark End-of-Line in MacOS and Linux systems.

The \r and
are known as the Carriage Return (CR) and Line Feed (LF) characters respectively. It is important to handle End-of-Line characters properly for consistency across multiple Operating Systems.

End−of−Line characters can be configured in two ways −

  • At the time of installation 

At the time of installation, Git allows us to select from the following options to configure the line ending conversions as shown above −

  • Checkout Windows-style, commit Unix−style line endingsCRLF will be converted to LF on checkout and commit operations. This is a recommended setting when Git is being installed on Windows and the project collaborators use non-Windows machines.

  • Checkout as−is, commit Unix−style line endings − No conversion will be performed on checkout. However, Git will convert CRLF to LF when the file is being committed. This is a recommended setting when Git is being installed on a Unix−style machine and the project collaborators use Windows machines.

  • Checkout as−is, commit as−is − No conversions are performed are performed when checking out or committing the files. This option is recommended when the Operating System on which Git is installed and the Operating System of the collaborators’ machine are the same.

  • After Installation − Configure the autocrlf property

The "core.autocrlf" property should be configured to manage End−of−Line conversion. Consider the following scenarios

Scenario 1 − Git is installed on a Windows machine and collaborators work on different platforms

Mr. A and Mr. B are working on the same project. Mr. A uses a Windows machine and Mr. B uses a Mac machine. As mentioned earlier, Windows and Mac Operating Systems use different characters to mark the End-of-Line. This means, if Mr. A checks in code into the repository, Git should remove the Carriage Return (\r) character from the End-of-Line. Similarly, when he checks out code from the repository, Git should update the End−of−Line and add the Carriage Return character.

On a Windows OS, this can be achieved by setting "core.autocrlf" to true by using the following command −

$ git config --global core.autocrlf true

Now let us create a file, add it to the staging area and observe the output.

$ echo hello >abc.txt
$ git add abc.txt

Git will display a message stating that LF will be replaced by CRLF in the file “abc.txt”. This is shown below −

Warning: LF will be replaced by CRLF in abc.txt.
The file will have its original line endings in your working directory

Scenario 2 − Git is installed on a Unix or Mac machine and collaborators work on different platforms

Let’s now consider a scenario where Mr. B, who uses a MacOS machine and tries to check out the code. Git should not modify the End-of-Line character if code is being checked out to a Mac or Linux machine. However, at times, the code editor or IDE may accidentally add a Carriage Return character. In such a case, Git should be smart enough to identify the Operating System and remove the Carriage Return character before adding it into the repository. This behavior can be set on a Mac or Linux system by setting the following line in the configuration file −

$ git config --global core.autocrlf input

This means, Git should only modify code when it is being stored in the repository.

Now let us create a file, add it to the staging area and observe the output.

$ echo hello > abc.txt
$ git add abc.txt

The output will be −

Warning − LF will be replaced by CRLF in abc.txt.
The file will have its original line endings in your working directory

Let us now try and observe Git’s behaviour if we add a file after disabling the core.autocrlf setting. The following command can be used to disable this setting.

$ git config --global core.autocrlf false

Now let us create a file, add it to the staging area and observe the output

$ echo hello > abc.txt
$ git add abc.txt

No warning message is now displayed by Git as autocrlf is set to false. This setting should not be used if the project is developed by collaborators using different platforms.

dell@DESKTOP−N961NRS VINGW64 /e/tut_repo (master)
$ echo hello > def. txt
dell@DESKTOP−N961NR5 MINGW64 /e/tut_repo (master)
$ git add def.txt

Updated on: 20-Feb-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements