Skip to content

Instantly share code, notes, and snippets.

@andrerom
Last active June 24, 2021 00:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrerom/fe44163de069dd6209169f515a9f4594 to your computer and use it in GitHub Desktop.
Save andrerom/fe44163de069dd6209169f515a9f4594 to your computer and use it in GitHub Desktop.
Git and how to handle line endings usage for mixed Windows and Mac/Linux clients
# Taken from: https://github.com/alexkaratarakis/gitattributes
#
# Auto detect text files and perform LF normalization so files in repo had LF and client translates to CRLF on Windows
* text=auto
# As a bonus also give hints to git on what diff to use
*.cs text diff=csharp
*.cshtml text diff=html
*.csx text diff=csharp
*.sln text eol=crlf merge=union
*.csproj text merge=union
*.js text
*.css text diff=css
*.scss text diff=css
*.gitattributes text
.gitignore text
*.md text diff=markdown
# Files where we force line ending also on client side
*.sln text eol=crlf
*.csproj text eol=crlf
*.vcproj text eol=crlf
*.sh text eol=lf
# TODO: Might need to add images we use and mark them as binary, unsure what git might already understand by default when using this
# see: https://github.com/alexkaratarakis/gitattributes/blob/master/Common.gitattributes#L35
# Exclude files from exporting
.gitattributes export-ignore
.gitignore export-ignore
.gitkeep export-ignore
# Enable syntax highlighting for files with `.gitattributes` extensions.
*.gitattributes linguist-language=gitattributes

Intro: Line endings and Git

In order to avoid issues withg line endings, Git suggests repo should only contain LF (Linux/Mac) line endings, only with a few exceptions for Windows only files.

Renormalize repo

https://git-scm.com/docs/gitattributes/en#_end_of_line_conversion

It suggests doing:

$ echo "* text=auto" >.gitattributes
$ git add --renormalize .
$ git status        # Show files that will be normalized
$ git commit -m "Introduce end-of-line normalization"

And then check each and every file if they should be converted or not, and if they are binary and should be ignored.

Git config for this

https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration#_formatting_and_whitespace

This implies Windows users needs the following config: $ git config --global core.autocrlf true

While Mac/Linux needs config to be: $ git config --global core.autocrlf input

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