Recently, when I used sourceTree for work, I often found that although it was only a small code change, there were inexplicably many more Spaces in the diff area of the file. Even if I chose to ignore the Spaces, there was still a high probability of errors in each item submitted, which was very annoying. The original idea was to script away the whitespace, but after a lot of effort, the problem was not completely solved.

Accidentally, I found a large number of ^M symbols when DIff was executed on the command line of the file submitted by my colleague. Considering that my colleague was using Windows, I finally realized that the problem was probably the newline character. Finally, they found that line Hamid was Set to Windows (CRLF) in Sublime and the main IDEA was set to LF. The result is a messy line feed for the project. The rest is focused on solving the problem.

background

First, the newline character varies from operating system to operating system. Linux uses 0x0A (LF), Windows uses 0x0D0A (CRLF), and MAC OS uses 0x0D (CR) at first and then the same as Linux. Git uses the Linux newline character by default (not surprisingly, of course).

Git resolves the problem of inconsistent newlines on different platforms by default converting LF to CRLF when checking out code and LF when committing code on Windows, but this seemingly perfect solution fails in Chinese environments.

The solution

Set git global parameters

Git has three parameters related to newlines:

  • eolLf, CRLF, and native (default, same as operating system)
  • autocrlf:
    • trueIndicates that the checout is converted CRLF and converted to LF at commit time
    • inputIndicates that the check out is not converted, and converted to LF at commit time
    • falseIndicates no conversion
  • safecrlf:
    • trueIndicates that different newlines are not allowed in the commit
    • warnWarning only if there are different newlines
    • falseAllows the existence of different newlines when raising prices

Configuration method:

<! Git config --global core.eol lf <! Git config --global core. Trigger lffalse<! Git config --global core.safecrlf -- git config --global coretrue
Copy the code

Add the configuration file. Gitattributes

Setting git global parameters fixes the problem, but working as a team, there is no guarantee that everyone is properly configured. Git provides a. Gitattributes file to solve this problem. Create a new. Gitattributes file in the project root directory and add the following:

# Set the default behavior, in case people don't have core.autocrlf set.
* text eol=lf
Copy the code

This prevents someone from setting the core-.autocrlf parameter and adding the file to version control.

In addition,.gitAttributes files can be created in different project directories as needed, while some non-text files can be set to binary files without worrying about newlines.

The existing content of the project is converted to a newline character

It is recommended to use the Dos2UNIX tool to convert the existing content of the project. Brew can be easily installed on MAC, and Git bash also comes with this tool on Windows. Take the Java source code in the transformation project as an example:

cd <project_path>
find . -type file -name '*.java' -exec dos2unix {} +
Copy the code

Finally, make sure the newline is set correctly when using the editor