21

I've been trying to set specific files to always use lf and not crlf regardless of autocrlf on the local system.

I tried creating a .gitattributes at the root of the project that only contained SquishIt.Tests/js/*.js eol=lf and I also tried SquishIt.Tests/js/ eol=lf. I pushed both of these attempts to my remote and then tried cloning it locally twice. The files under /js/ always showed up with CR+LF in both cases as autocrlf is on globally for me.

I'm on a Windows machine, just in case it was unclear. Is what I'm attempting to achieve even possible?

Akkuma
  • 2,215
  • 3
  • 16
  • 21

2 Answers2

28

I wanted to have all text files forced to use LF except one sub directory (.idea) which is forced to use 'CRLF'. This is how my .gitattributes looks:

* text eol=lf
/.idea/* text eol=crlf

So I assume yours should look like:

* text=auto
/SquishIt.Tests/js/* text eol=lf
Ben
  • 3,255
  • 1
  • 26
  • 32
  • 1
    I happen to come here exactly for this .idea - I would appreciate if you added a link to some docs :) In particular why the leading `/` ? Also how can one include files in subfolders like `.idea/dictionaries` – Mr_and_Mrs_D Mar 24 '17 at 18:51
  • 1
    To your second question, you can simply use /.idea/dictionaries/* to match files in this sub directory. – Tarnschaf Apr 12 '17 at 14:25
  • 3
    If you want to include files in a folder and ALL subfolders, use `/.idea/**`. If you want to include files in ANY subfolder named .idea regardless of depth, use `.idea/*`. – SensorSmith Jan 20 '18 at 00:06
  • Is it better to have one unique `.gitattributes` in the root folder, but with heavy syntax naming each sudirectory? Or to have multiple gitattributes, each in one subdirectory, but to be maintained individually... ? – Sandburg Mar 24 '22 at 13:39
-2

I would set autocrlf to false and set core.whitespace to cr-at-eol. It's better to let your editors deal with the difference in line endings. Let git store them as they are.

Hope this helps.

Adam Dymitruk
  • 124,556
  • 26
  • 146
  • 141
  • There are unit tests that read back the contents of files and the files must maintain its normalized line endings across all platforms regardless of autocrlf settings as to properly ensure the tests pass. I can't control whether or not contributors have these settings. – Akkuma Jul 13 '11 at 14:16
  • Make the tests more resilient. I would not want anything like vcs interfere with what a file contains. – Adam Dymitruk Jul 13 '11 at 17:42
  • 1
    Thanks, I figured out a way to make the tests more resilient without having to worry about the vcs anymore. For those interested, I basically read the file in, normalize the line endings, and rewrite the file to guarantee it has the correct line endings. – Akkuma Jul 13 '11 at 18:10
  • Please do not accept this response as the answer, as it doesn't answer your question. There are some situations which you would absolutely want to control line endings on a subdirectory. For example, I have a puppet project contained in my application that *must* have LF line endings for all its files, or else it will break. – Paul Gibler Aug 24 '14 at 14:57
  • Another valid usecase: I've got a folder of my repo on my Windows OS that is shared with a Linux VM. Included in this folder are shell scripts for the Linux guest to run. I need git to check out with Linux EOL instead of the default Windows EOL. – thelr Mar 04 '15 at 13:39
  • And then devs with different editors or different OS will commit in different formats, causing diffs to be unreadable ... – fons Jun 08 '17 at 06:30