Git -- Set text=auto and eol=lf in gitattributes

Viewed 463

I want git to normalize text files with LF line endings using a .gitattributes file in my repo.

I've tried * text=auto, however, that checks out files on Windows with CRLF line endings, converting them back to LF upon adding the changes to Git.

I've also tried * eol=lf, but that also sets the line ending for files that aren't text, like .jar files.

I've tried adding both the same line (* text=auto eol=lf), but that just did the same as having * eol=lf.

Is there a way to normalize line endings of only text files using .gitattributes, that doesn't checks them out as LF on Windows platforms?

1 Answers

Git has some heuristics to guess what a text file is, but since it doesn't always guess correctly, you can specify which types of files you want to affect. For example, if you're working on a Java project, you might write the following

*.java text
*.jar -text

That will cause Java files to be checked out using CRLF on Windows and LF on Unix, while storing LF in the repository. If you want to use LF on all systems, write something like:

*.java eol=lf
*.jar -text

Specifying -text means that you don't want those files to be affected by core.autocrlf, which is the option that makes Windows systems see CRLF while keeping LF in the repo. You can combine multiple of these types of lines to affect different files.

Related