| by Arround The Web | No comments

Git Attributes: Customizing Git Behavior and Metadata

Whenever the project file is created with code in Git Bash, the specific EOL (End of Line) is assigned to it. Currently, there are 2 main types of EOL, LF (Line Feed) and CRLF (Carriage Return Line Feed). Git behaves differently. These end-line types vary depending on the operating system. Linux’s operating system supports LF while Windows supports CRLF.

Multiple users working on projects from different operating systems may create problems sometimes. For that purpose, the user can customize the line ending behavior using the “.gitattributes” file.

At the end of this blog, we promise you will have a deep knowledge of the “.gitattributes” file to customize the Git behavior for managing the files.

The outcomes for this blog are:

How to Customise Git Behaviour For Line-Ending?

To customize Git for line-ending behavior, you can simply create the “.gitattributes” in the project directory and define the end line type. For in-depth knowledge, let’s create the file, check its line ending type, and change it. Just walk through the following steps with us.

Step 1: Move to Project Directory

Open the terminal and move to the project directory by running the “cd” command. As we have moved to the “testing” directory:

cd testing

Step 2: Create a File and Edit

Create the new file using the “touch” command and edit it with the nano editor:

touch file.txt

nano file.txt

The file “file.txt” is created.

For instance, let’s add the 2 lines in our “file.txt”:

this is the line 1

this is the line 2

Save the content of the file by pressing “Ctrl+O” and exit from the file by pressing “Ctrl+X”.

Step 3: Track File

Track the changes in the file by executing the “git add” command:

git add .

The directory content has been tracked.

Step 4: Verify Status

Verify the status of the file using the “git status” command:

git status

Step 5: Check the Line of Ending Type

In order to check the line ending type of the file, run this command in the terminal:

git ls-files --eol

You can see that the current file type is LF. As we have mentioned above Linux supports the LF (Line Feed) ending line.

Step 6: Create Attributes File

Let’s assume you want to convert this LF into CRLF. For that purpose, create the “.gitattributes” files in your directory and define it. For instance, we are creating and editing the “.gitattributes” using the “nano” editor:

nano .gitattributes

Step 7: Define File Ending Type

A nano text editor will be opened, as we have the “.txt” file extension so we are using following line to convert the line ending type to CRLF:

* text eol=crlf

Save the file and exit from the editor.

Step 8: Re-check the File Ending Type

Now, let’s re-check the file ending type again for the text file in our directory:

git ls-files --eol

You can see that the behavior of the line ending type in a file has been changed to CRLF because of the “.gitattributes” file.

Why does Windows Git Bash display a Warning of Converting LF to CRLF?

Yes, the Windows Git Bash displays a warning for converting ending line type LF to CRLF because it only supports the CRLF. the warning can be seen in the following output of the “git add” command:

Let’s verify that Windows understands and converts the CRLF format only. For this purpose, clone the remote repository and check its line ending type for files. See the following 2-steps.

Step 1: Clone Project

Cloning the project via the “git clone” command along with the repository remote URL:

git clone https://github.com/Mateen900/perk

Our project has been cloned.

Step 2: Check the Type of the Ending Line

Let’s check any file for line ending type. For now, we have the “index.html” file:

file index.html

You will see that Windows understands the CRLF ending line format.

Conclusion

To customize the Git files behavior for line ending type, create the “.gitattributes” in a file in the project directory and use this syntax “* <file type> eol=<line ending type>”. In this guide, we have deeply understood the different line ending types for the files and the steps to customize them using the “.gitattributes” file.

Share Button

Source: linuxhint.com

Leave a Reply