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.

5
  • I don't understand your question. Git doesn't modify files. Commented May 12, 2015 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. Commented May 12, 2015 at 15:25
  • @isherwood it's exactly as chepner said, have a look at help.github.com/articles/dealing-with-line-endings/… Commented May 12, 2015 at 17:31
  • I stand corrected. Thanks. Commented May 12, 2015 at 18:08
  • possible duplicate of How do I force git to use LF instead of CR+LF under windows? Commented May 20, 2015 at 9:51

2 Answers 2

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 
Sign up to request clarification or add additional context in comments.

3 Comments

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?
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.
ok, so it behaves similar to .gitignore. I'll keep you posted, thanks.
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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.