btilly
btilly

Reputation: 46399

Git messing up line endings despite .gitattributes

I have already seen gitattribute end of line setting and this is a different and quite bizarre problem. I have already checked my autocrlf and eol configurations and they are not set

I'm on a Mac. In our repository we have a .gitattributes file that looks like this:

*.sql text eol=lf
*.sh text eol=lf

This works perfectly for .sql files. However we have a file named scripts/some-version.SQL. It is checked in with Unix lf endings. When I check it out on OS X, that file immediately gets converted to dos crlf endings and is marked as changed. Checking the same file out on Windows gives a Unix lf ending.

If I comment out the .sql line in the .gitattributes file, I stop getting the .SQL file checked out as crlf. It is the .gitattributes that is causing this behavior. However then Windows users will accidentally start checking in crlf endings again. Changing the filename is sadly not an option.

I tried adding a .SQL text eol=lf line to .gitattributes but that did not help.

How can we have a .gitattributes for our Windows users without messing up the line endings for this file on OS X?


Per request. A demonstration of the issue, along with check-attr.

macbookpro:postgres btilly$ git checkout scripts/
Updated 1 path from the index
macbookpro:postgres btilly$ git status scripts/3.20.08-UPDATE.SQL 
On branch integration
Your branch is up to date with 'origin/integration'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   scripts/3.20.08-UPDATE.SQL

no changes added to commit (use "git add" and/or "git commit -a")
macbookpro:postgres btilly$ git check-attr -a scripts/3.20.08-UPDATE.SQL 
scripts/3.20.08-UPDATE.SQL: text: set
scripts/3.20.08-UPDATE.SQL: eol: lf
macbookpro:postgres btilly$ file scripts/3.20.08-UPDATE.SQL 
scripts/3.20.08-UPDATE.SQL: UTF-8 Unicode text, with very long lines, with CRLF line terminators

Upvotes: 1

Views: 1478

Answers (1)

bk2204
bk2204

Reputation: 76429

It is likely that your file has been checked in with CRLF line endings, at least in some places. Much to people's surprise, editing .gitattributes doesn't affect any files that are checked in to the repository already, and it doesn't affect clean files in the working tree.

Basically, anytime you make a change to .gitattributes, you need to run git add --renormalize . and commit those changes as well. Otherwise, you can end up with things in an inconsistent state.

Note also that your .gitattributes pattern will not work on systems that are case sensitive, since your pattern uses the lower case *.sql and your file has the upper case .SQL. So you'll probably want to add this as well:

*.SQL text eol=lf

Of course, you'll want to do that before running git add --renormalize ..

Upvotes: 3

Related Questions