9

I use git-svn at work and people are constantly complaining that I'm committing LF's instead of CRLF's. (We're mostly a Windows shop).

As far as I can tell, when core.autocrlf is true the working copy is CRLF, when false it's LF, and when input it's left untouched. I like the autocrlf = true as a concept, but I wish it would default to CRLF in the index since that's what get's committed to SVN.

Is there a way to set what line ending the index uses?

I've seen core.eol, but this also seems to only set what the working copy uses, not the index.

kelloti
  • 8,705
  • 5
  • 46
  • 82

3 Answers3

2

You'll want to add a file named .gitattributes to the root of your project. This will force everyone to commit same line ending. In you case, as you want to force crlf, you'll add this line in the .gitattributes:

* text eol=crlf

Then, you can also normalize every files in your repo once, and never bother about it again: Trying to fix line-endings with git filter-branch, but having no luck

You can also refer to github guide on line ending: https://help.github.com/articles/dealing-with-line-endings

Community
  • 1
  • 1
Simon Boudrias
  • 42,953
  • 16
  • 99
  • 134
  • Can I do this without a `.gitattributes` file? Maybe a `.git/info/attributes` file instead? (Since I'm using `git-svn`) – kelloti Feb 04 '13 at 18:26
  • Yes, you can also put this setting in this file and have the same effect (only for you). – Simon Boudrias Feb 04 '13 at 18:29
  • You can checkout the section on file precedence on the git documentation: http://www.kernel.org/pub/software/scm/git/docs/gitattributes.html – Simon Boudrias Feb 04 '13 at 18:30
  • ok, I'm having trouble with the `git diff ... | xargs...` line. It says `xargs: argument line too long`. I'm not sure I want to make that many changes anyway... – kelloti Feb 04 '13 at 18:34
  • 1
    I don't think this even answers the question - the docs say this is just like setting `core.autocrlf = true` but only on certain files (in this case only text files, so it's absolutely no different). – kelloti Feb 04 '13 at 18:39
  • By specifying `*`, you take every files. And by text files, they mean not `binaries` files, and not _only_ `*.txt`. Also, setting `autocrlf` to true will always commit files as ending in `lf`, that's why we manually set them to be `crlf`. – Simon Boudrias Feb 04 '13 at 18:41
  • 4
    For `eol=crlf` it says `This setting forces git to normalize line endings for this file on checkin and convert them to CRLF when the file is checked out.` To me this means that it's still normalizing to `LF` in the index (vague as it reads) and convert to `CRLF` in the working copy. But I want it to normalize to `CRLF` in the index. Is that even possible? – kelloti Feb 04 '13 at 18:49
  • You can test it out. `git diff` will point out if there's changes to the line-ending. If it's not working, then just set `core.autocrlf` to false and Git won't change line-ending anything automatically; this will work as long as every developer is on windows. – Simon Boudrias Feb 04 '13 at 19:06
  • 2
    It seems the person asking the question wants to store CRLF in the index. This does not do that. – Daniel Leach Oct 17 '17 at 19:09
1

It turns out that git-svn uses the svn:eol-style property from SVN to decide how to store the line endings. If you go into a pure SVN checkout & add svn:eol-style set to native, Git will use CRLF for Windows and LF for Linux/OSX.

I also have these settings:

$ git config core.autocrlf false
$ git config core.safecrlf true

I don't have anything in my .gitattributes file.

kelloti
  • 8,705
  • 5
  • 46
  • 82
1

I know this question is old, but just in case someone is still looking for an answer...

First, you have to make sure that core.autocrlf is false in your global and local config. Then to force git to convert to CRLF, you can use a filter defined like this :

In your local config file (.git\config) :

[filter "crlf"]
        clean = unix2dos

And you can use it by adding a filter for certain files in the attribute file (.git\info\attributes) :

*.txt filter=crlf

This filter makes use of the command unix2dos which comes with Git for Windows and could already be available if you are on Linux. Otherwise, you can install it or use an alternative command like sed -i -e 's/\r//g' (this command works well assuming that you don't have any Mac line endings in your files).


What about running the filter on all files like this?

* filter=crlf

This could work, but I haven't used it in a large repo, so I can't guarantee that it won't affect performance or that there is no risk of corrupting binary files.

Luckily, unix2dos has some checks in place to detect binary files and won't make any changes to them if they are detected as binary. I tried to run it on a PNG and got:

unix2dos: Binary symbol 0x1A found at line 2
unix2dos: Skipping binary file fff.png

You might need to keep an eye open if some files aren't detected properly, however.

DecimalTurn
  • 3,243
  • 3
  • 16
  • 36