Dimitrios K.
Dimitrios K.

Reputation: 1028

Can I commit specific files in git without affecting the original line ending?

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.

Upvotes: 0

Views: 529

Answers (2)

Dimitrios K.
Dimitrios K.

Reputation: 1028

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

Upvotes: 0

chepner
chepner

Reputation: 530940

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

Upvotes: 1

Related Questions