Problem
VSCode uses CRLF
for windows by default, while mac is LF
.
When collaborating across platforms, even if save and submit the same file, we will find that it diffs the entire file.
What is CR and LF?
- LF (Line Feed) stands for
\n
. On Linux and Mac, this corresponds to the beginning of a new line of text. This distinction is important, because Windows does not follow this convention.- CR(Carriage Return) stands for
\r
. Moves the cursor to the beginning of the current line. The download progress bar on your terminal is achieved through CR. By using the carriage return character, your terminal can animate the text by returning the cursor to the beginning of the current line and overwriting any previously rendered text.
Solutions
Convert files Configuration set by Git
Referance: https://stackoverflow.com/questions/1967370/git-replacing-lf-with-crlf
The value of core.autocrlf needs to be configured manually, due to the default is false on Windows.
The problem can be partially solved by configuring it as true/input.
input is better.
It only works for new files, the existing CRLF files in the repository are still CRLF.
Use VSCode plugin EditorConfig for VS Code
Install VSCode plugin EditorConfig for VS Code and create configuration file .editorconfig.
It can ensure that manually re-save/create files are LF by default, but old ones need to be all re-saved for them to take effect.
- VSCode plugin EditorConfig for VS Code:
Create configuration file .editorconfig:
root = true
[*]
end_of_line = lfCommit the configuration file .editorconfig to the repository.
The configuration used in my project is like:root = true
[*]
charset = utf-8
indent_style = space
indent_size = 4
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
[*.md]
insert_final_newline = false
trim_trailing_whitespace = false
Batch conversion of existing CRLF
files to LF
VSCode does not provide a method for batch conversion, only individual file conversion.
We can use Dos2Unix for batch conversion.
Installation package: https://waterlan.home.xs4all.nl/dos2unix/dos2unix-7.4.3-win64.zip
After installation, add it to the environment variable Path
Start cmd, switch to the project directory and execute this command:
for /R %G in (*.js *.json *.md) do dos2unix "%G"
*.js *.json *.md used to decide which types of file you need to Convert.
Summary
To All
Set the value of autocrlf as input
git config core.autocrlf input
git rm --cached -r .
git reset --hardInstall VSCode plugin EditorConfig for VS Code.
To Me
- Use Dos2Unix to batch convert all existing files from
CRLF
toLF
. - Commit all modified files to the repository