Post

Line Break Differences - Windows and Linux EOL Check and Git Repo Sync

If you need to modify and commit files in one Git repo on both Linux and Windows, there may be cases where the Git client recognizes the file as a modified file at the Git commit stage even though the content has clearly not been modified.

The cause of the problem is that although the file contents are the same, the basic way of expressing line breaks in Windows and Linux is different. In the picture below, the left is a file modified and saved in Windows (PC), and the right is a file modified and saved in Linux (UNIX).

Same file contents but different files, on the left is Windows PC format and on the right is Linux UNIX format Same file contents but different files, on the left is Windows PC format and on the right is Linux UNIX format

Differences of line breaks between Windows and Linux

From an ASCII character perspective,

Windows expresses line breaks in the form of CRLF (Carriage Return with Line Feed) as shown below.

1
\r\n

In Linux systems such as Ubuntu, line breaks are expressed only as LF (Line feed) as shown below.

1
\n

Assume that you received a file expressing newlines in LF format from Git, modified it briefly in a Windows editor, and then restored it. This means that the file has the same content on the surface, but the line breaks are modified with CRLF, so there is a possibility that it will be transformed into a different file from the file in Git.

If you run a bash script written on Windows on Linux, you may encounter mysterious script execution errors during debugging. This may be because the shell script stored line breaks in CRLF format on Windows and bash could not interpret this.

Check and convert EOL (End Of Line) of a file

Of course, even if you write a file in Windows, you can create and convert the file in UNIX (LF) format, not PC (CRLF) format. The opposite is also possible. The conversion operation is commonly called EOL (End Of Lines) Conversion.

In fact, it can be a little confusing when you first encounter the differences in EOL for each OS. So, keywords related to the content to be explained in the future can be mapped to a table as shown below.

NameWindowsUbuntu (Linux)
line breaks format terminologyCRLF (Carriage Return with Line Feed)LF (Line feed)
line breaks representation characters\r\n\n
Information from the file tool in UbuntuASCII text, with CRLF line terminatorsASCII text
EOL type in Beyond Compare toolPCUNIX
EOL type in Ubuntudosunix
EOL Conversion Tool on Ubuntuunix2dosdos2unix

Windows

Using the Notepad++ editor can help you create files with EOL in mind.

As shown in the picture below, you can check the current EOL Conversion status in Notepad++ editor and convert. The menu is in Edit > EOL Conversion.

Notepad++ - EOL Conversion - `Edit > EOL Conversion > Windows (CR LF) Or Unix (LF)` Notepad++ - EOL Conversion - Edit > EOL Conversion > Windows (CR LF) Or Unix (LF)

Ubuntu (Linux)

In the Ubuntu environment, you can also check the EOL status of files and convert them.

You can check the EOL status of a file using the file tool.

1
2
3
4
5
6
7
# PC (CRLF)
$ file pc.txt 
pc.txt: ASCII text, with CRLF line terminators

# Unix (LF)
$ file unix.txt 
unix.txt: ASCII text

For EOL conversion of files, tofrodos and dos2unix must be installed. It can be installed using apt.

1
2
sudo apt-get install tofrodos
sudo apt-get install dos2unix

Once you install the packages, you can use the tools below.

  • dos2unix : Convert PC (CRLF) to Unix (LF)
  • unix2dos : Convert Unix (LF) to PC (CRLF)

Below is an example of converting PC (CRLF) to UNIX (LF).

1
2
3
4
5
6
7
8
9
$ file pc.txt 
pc.txt: ASCII text, with CRLF line terminators

$ dos2unix pc.txt 
dos2unix: converting file pc.txt to Unix format...

$ file pc.txt 
pc.txt: ASCII text

Git repo EOL synchronization between OSs

There is a Git setting in one Git repo that prevents EOL conversion issues when committing and pulling files in Windows and Ubuntu environments.

The git command below changes CRLF to LF before inserting an ASCII format file into the object database. So it is mainly used for Git settings in a Windows environment.

1
git config --global core.autocrlf input 

The git command below uses line breaks as LF. So it is mainly used in Ubuntu (Linux) environment.

1
git config --global core.autocrlf true

Since the above command is a global Git setting, if you want the EOL settings to be applied only to a specific cloned repo, you can go into the relevant repo folder and use the command with only the --global option removed.

In other words, the intention is to upload files with an EOL in LF format to the Git server.

In a Windows environment, you can access Git Bash from the menu after right-clicking.

Accessing Git Bash on Windows Accessing Git Bash on Windows

This post is licensed under CC BY 4.0 by the author.