Git

Why Should I Use core.autocrlf=true in Git?

While working as a team on Git projects, it might be possible project members are utilizing different operating systems. Due to this conflict, they encounter line-ending issues because text files created on Windows have different line endings than Linux text files. Windows uses CR (Carriage-Return) and LF (LineFeed) characters for new lines in its files, while Linux systems use only the LF character. More specifically, Git does not permit the developers to use UNIX-style LF while working on Windows.

This write-up will briefly explain the usage of the core.autocrlf=true in Git.

Why Should You Use core.autocrlf=true in Git?

To collaborate efficiently with other project developers using different operating systems, developers must change configuration settings to set up Git to handle line endings automatically. To do so, utilize the “$ git config core.autocrlf=true” command for changing the core.autocrlf settings. Windows users need to set core.autocrlf value to true for converting LF endings into CRLF.

How core.autocrlf=true Works in Git?

Let’s take an example to see how core.autocrlf=true works!

Step 1: Navigate to Git Repository

First, move to the particular Git repository using the “cd” command:

$ cd "C:\Git"

Step 2: Create Two Files

Next, create a file with the help of the “echo” command and update it:

$ echo "File 3" > File3.txt

Similarly, generate or update another file using the same command:

$ echo "File 4" > File4.txt

Step 3: Add Files to the Git Staging Index

Next, utilize the “git add” command to track files to the staging index:

$ git add File3.txt File4.txt

In the below output, a warning can be observed that says “LF will be replaced by CRLF”.

Note that the LF is the UNIX-style and CRLF is the Windows style. This warning states that you will lose UNIX-style, and it will be replaced with Windows-style as Git restricts the use of CRLF by default:

Step 4: Check Default Configuration Settings

To check the default configuration settings, run the following command:

$ git config core.autocrlf

It can be observed that the default value of the “core.autocrlf” file is set as “false”:

Step 5: Change core.autocrlf Configuration

To set the Git “core.autocrlf” setting to “true”, run the following command:

$ git config core.autocrlf true

Step 6: Verification

Verify the previously performed configuration changes:

$ git config core.autocrlf

You can see that the “core.autocrlf” value has been set as “true”:

Step 7: Track Changes to Git Staging Area

Again, try to add the files to the Git staging area:

$ git add .

As you see in the below-given screenshot, the files have been added successfully because the “core.autocrlf” settings are changed to true:

We have explained the usage of the core.autocrlf=true configuration setting in Git.

Conclusion

While working with developers with different OS systems, users probably encounter line-ending (LF or CRLF) issues. Git provides various ways to resolve this issue, such as using the “$ git config core.autocrlf” command. If your core.autocrlf configuration is set as false, this will show you warning of line endings issues while adding files. However, setting its value as “true” will solve the issue. This write-up demonstrated the usages of core.autocrlf=true configuration setting in Git.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.