Monday, February 12, 2018

git and line endings, .gitattributes

If you develop in Windows, and deploy to Linux, and use git,
you may run into issues where Linux configuration files end up with Windows new lines ie carriage return and new line ie \r\n, thus preventing your Linux configuration from working.

Note: Check if you have the default git option of config.autocrlf=true set, or are required to by your company policy.

To keep your Linux configuration files 'clean' with just new lines ie \n
you can override certain directories or files to use a specific line ending, such as \n.
Git provides additional settings by placing a file named
.gitattributes
in either your code repository root, or in a subdirectory.

Note: Windows explorer will not allow you to create a dot file ie .gitattributes or .htaccess
You can copy an already created or downloaded file, and edit it, or from the command line
> echo > .gitattributes
or
> rename newdocument.txt .gitattributes

To let Git handle the automagic line endings conversion for you, on commits and checkouts. Binary files won't be altered, files detected as being text files will see the line endings converted on the fly.
> echo .gitattributes
* text=auto

You can also add additional criteria to specify whether a file is text, thus auto convert line endings, or binary, do nothing.
> echo .gitattributes
# text; auto convert line endings
*.txt text
*.h text

# binary; do not do anything
*.jpg binary
*.data binary

You can also ensures that all files that match a pattern have normalized line endings in the repository by adding a eof property; eof can be lf or crlf
> echo .gitattributes
# Ensure shell and conf files use LF.
*.sh         eol=lf
*.conf       eol=lf
*.bash       eol=lf

A large example of .gitattributes on GitHub

End of document. Thanks for reading.

No comments:

Post a Comment