Skip to content

Instantly share code, notes, and snippets.

@starise
Last active September 13, 2017 21:56
Show Gist options
  • Save starise/a35b04a31cccc8538d4718325ffe6b48 to your computer and use it in GitHub Desktop.
Save starise/a35b04a31cccc8538d4718325ffe6b48 to your computer and use it in GitHub Desktop.
Proper way to handle line endings in Windows

Best way to handle line endings in Windows

Set core.autocrlf to false and default line endings to LF globally for git:
git config --global core.autocrlf false
git config --global core.eol lf
Set text=auto in your .gitattributes for all files:
# Handle line endings automatically for files detected as text
# and leave all files detected as binary untouched.
* text=auto

Normalize existing repositories

After above configuration, you might want git to normalize all the files in the repo. To do this, go to to the root of your repo and run these commands:

git rm --cached -rf .
git diff --cached --name-only -z | xargs -n 50 -0 git add -f
git ls-files -z | xargs -0 rm
git checkout .

Now just be sure that all of your text editors and IDEs use LF and UTF-8.

Optional: switch to crlf on per-repository basis:

You can switch to CRLF for a single repository without changing the global settings. Git will handle the LF convertion thanks to the .gitattributes file.

git config core.eol crlf

Optional: create .editorconfig file into your repo:

Define your custom coding styles using EditorConfig files:

# editorconfig.org

root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

Source: https://stackoverflow.com/a/13154031

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment