Thursday, 27 December 2018

GIT: Handling line endings on different platforms

51.1. Line endings of the different platforms

Every time a developer presses return on the keyboard an invisible character called a line ending is inserted. Unfortunately, different operating systems handle line endings differently.
Linux and Mac use different line endings than Windows. Windows uses a carriage-return and a linefeed character (CRLF), while Linux and Mac only uses a linefeed character (LF). This becomes a problem if developers use different operating system to commit changes to a Git repository.
To avoid commits because of line ending differences in your Git repository you should configure all clients to write the same line ending to the Git repository.

51.2. Configuring line ending settings as developer

On Windows systems you can tell Git to convert line endings during a checkout to CRLF and to convert them back to LF during commit. Use the following setting for this.
# configure Git on Windows to properly handle line endings
git config --global core.autocrlf true
On Linux and Mac you can tell Git to convert CRLF to LF with the following setting.
# configure Git on Linux and Mac to properly handle line endings
git config --global core.autocrlf input

51.3. Configuring line ending settings per repository

You can also configure the line ending handling per repository by adding a special .gitattributes file to the root folder of your Git repository. If this file is committed to the repository, it overrides the core.autocrlf setting of the individual developer.
In this file you can configure Git to auto detect the line endings.
Not all graphical Git tools support the .gitattributes file, 
for example the Eclipse IDE does currently not support it.

0 comments:

Post a Comment