Git

How to Fix LF Will Be Replaced by CRLF Warning in Git

While using Git, sometimes you may encounter multiple errors and warnings while performing different tasks, such as creating new projects files and folders in different scenarios, such as while tracking files from the working directory to the staging area. The “warning: LF will be replaced by CRLF” is the most common warning you may face while adding files to Git local repository.

This article will discuss the “warning: LF will be replaced by CRLF” and provide the solution to fix it.

What is “warning: LF will be replaced by CRLF” in Git?

The “warning: LF will be replaced by CRLF” mostly occurs when the value of the Git configuration variable is settled as “true”. The reason behind the warning is that the committed files are different from what you saved.

So, to resolve or avoid this warning, you have to change the value of the Git configuration variable using the “$ git config <–global or local> core.autocrlf false” command.

Check out the below-given scenario, which shows us the specified warning.

Step 1: Navigate to Git Folder

First, move to the Git projects root folder using provided command:

$ cd "C:\Users\nazma\Git"

Step 2: Create and Move to Git Local Repository

Create a new directory by executing the “mkdir” command and immediately move to it utilizing “cd” command:

$ mkdir demo7 && cd demo7

Step 3: Initialize Git Repository

Now, run the “git init” command to initialize the Git local repository:

$ git init

Step 4: Open and Update File

Run the provided command to create a new file and add some text using the redirect operator:

$ touch file1.txt && echo "It's my first file" > file1.txt

Here, we have added “It’s my first file” text in “file1.txt” through “echo” command:

Step 5: Add File

Next, add the file to the Git staging area using below command:

$ git add file1.txt

It can be seen that Git bash threw the mentioned warning when we tried to add the file to the staging area:

Now, let’s move to the next section to fix the “warning: LF will be replaced by CRLF”.

How to Fix “warning: Lf will be replaced by CRLF”?

Now, let’s move toward the below-provided steps to fix the stated warning encountered in the previous section.

Step 1: Check Settings

First, check the default settings of the Git configuration variable value using the provided command:

$ git config core.autocrlf

According to the below output, the Git configuration variable is set as “true”:

Step 2: Replace by CRLF Warning Globally(Per User)

Next, we will change the value of the Git configuration variable to “false” with the help of given command:

$ git config --global core.autocrlf false

Step 3: Verify Settings

Now, we will verify the performed operation to ensure the configuration variable value is changed to “false”:

$ git config core.autocrlf

Below-output indicates that the variable value is successfully set to “false”:

Step 4: Replace by CRLF Warning Locally(For the Project Only)

To replace the CRLF warning for the specific local projects, execute the below provided command:

$ git config --local core.autocrlf false

Step 5: Verify Settings

Now, we will run the “git config” command with “core.autocrlf” Git configuration variable to check its configured value:

$ git config core.autocrlf

In the below output, you can see that value is successfully settled to “false”:

Step 6: Add File

Finally, we will add the file to the Git staging area with the help of the given command:

$ git add file1.txt

It can be seen that; the file is added successfully:

Step 7: Check Status

Lastly, we will check the Git repository’s current status:

$ git status .

The below output indicates that the new file is added to the Git repository:

That’s it! We have provided the simplest solution to fix the CRLF warning in Git.

Conclusion

Git users mostly encounter the “warning: LF will be replaced by CRLF” when the “autocrlf” Git configuration variable value is configured as “true”. However, you can change its value as “global” per user as well as “local” per project in the “$ git config <–global or local> core.autocrlf false” command. In this article, we have discussed the “warning: LF will be replaced by CRLF” and offered the solutions to fix it.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.