0

As the question states, we have some test CSV files that need to retain their line endings. These files are used later for testing a CSV parser.

Non-duplicate edit: I do not want to convert all files' ending to LF. I want to push specific files to Git end retain their original line ending, e.g. CRLF. So all other files would be normalized to LF which is Git's default while the CSV files will be committed with CRLF.

Dimitrios K.
  • 1,008
  • 13
  • 36
  • I don't understand your question. Git doesn't modify files. – isherwood May 12 '15 at 15:23
  • Git can modify the type of line endings used, both when adding the file to the repository and when checking a copy out into the working directory. – chepner May 12 '15 at 15:25
  • @isherwood it's exactly as chepner said, have a look at https://help.github.com/articles/dealing-with-line-endings/#global-settings-for-line-endings – Dimitrios K. May 12 '15 at 17:31
  • I stand corrected. Thanks. – isherwood May 12 '15 at 18:08
  • possible duplicate of [How do I force git to use LF instead of CR+LF under windows?](http://stackoverflow.com/questions/2517190/how-do-i-force-git-to-use-lf-instead-of-crlf-under-windows) – Martin G May 20 '15 at 09:51

2 Answers2

1

In .gitattributes, set the line-ending style to use for the specific files.

with-dos-line-endings.csv eol=crlf
with-unix-line-endings.csv eol=lf
chepner
  • 497,756
  • 71
  • 530
  • 681
  • Thanks, @chepner. I'll have a look. Does the .gitattributes file goes into the same folder as the CSVs, e.g. src/text/resources/.gitattributes? – Dimitrios K. May 12 '15 at 17:18
  • It can. See `man gitattributes` for a full explanation of how and where to specify attribute, but Git will can use `.gitattributes` either in the same folder, or any parent folder up to the repository root. – chepner May 12 '15 at 17:21
  • ok, so it behaves similar to .gitignore. I'll keep you posted, thanks. – Dimitrios K. May 12 '15 at 17:30
0

Following @chepner advice for the .gitattribute file I used him proposed configuration. It didn't work as expected so I did some more digging. Turns out the answer was in the Git manual! (RTFM, I know right?!).

Check the EFFECTS section on Git Manual.

eol
This attribute sets a specific line-ending style to be used in the working directory. It enables end-of-line normalization without any content checks, effectively setting the text attribute.

Set to string value "crlf"
This setting forces Git to normalize line endings for this file on checkin and convert them to CRLF when the file is checked out.

Set to string value "lf"
This setting forces Git to normalize line endings to LF on checkin and prevents conversion to CRLF when the file is checked out.

What I actually needed is -text. By using -text you ask Git to treat that file as a binary and not affect its line endings.

Unsetting the text attribute on a path tells Git not to attempt any end-of-line conversion upon checkin or checkout.

And the backwards compatible version:

Backwards compatibility with crlf attribute
For backwards compatibility, the crlf attribute is interpreted as follows:

 crlf        text  
-crlf       -text  
 crlf=input  eol=lf
Dimitrios K.
  • 1,008
  • 13
  • 36