4

One of our developers' text editor messes up the line endings of our PHP files. We've tried all settings, but the editor still seems to replace the CR-LF's with LF's on saving.

Of course, the most sane thing to do is replace the editor with a decent one, but I though: why not configure Git for handling the differences in line ending? Here's where I get stuck; I can't get it to work properly and I'd like your advice.

It seems the files we check out have CR-LF line endings. When our developer edits them and stores them, they same to have LF endings. When committing the file, not only do the lines change, but every line is removed and inserted again.

I've set core.autocrlf to true. When committing, I get a warning that all LF will be replaced by CR-LF. The file will have the original endings in your work directory. But still, all lines are removed and added again. When merging with another branch, this gives conflicts.

I tried making a .gitattributes file with *.php text. Same result as with core.autocrlf.

So my question is: is there a way to configure Git to only commit the changes our programmer's made and leave alone the lines where only the line ending has changed?

jub0bs
  • 60,866
  • 25
  • 183
  • 186
  • You should edit your question to add some information. Which platform are you and your developers using (Windows, *nix)? What is the problematic editor in question? – jub0bs Aug 23 '14 at 10:29
  • Have you read through this? https://help.github.com/articles/dealing-with-line-endings It suggests that once you've set up a .gitattributes file, github will want to change all the line endings. From the docs: "The best way to automatically configure your repository's line endings is to first backup your files with Git, delete every file in your repository (except the .git directory), and then restore the files all at once." – i alarmed alien Aug 23 '14 at 10:30
  • possible duplicate of [What's the best CRLF handling strategy with git?](http://stackoverflow.com/questions/170961/whats-the-best-crlf-handling-strategy-with-git) – jub0bs Aug 23 '14 at 10:36
  • Yes, I read the dealing-with-line-endings and The Best CRLF strategy articles, but I'm asking because it doesn't seem to do anything. Here's what I did just now: - Removed all files in workdir except the .git - Added and commited a [code].gitattributes[/code] file with [code]*.php text[/code] - Set the [code]core.autocrlf[/code] config to [code]true[/code] - did a [code]git reset --hard[/code] to have git create all files again – William van Veldhoven Aug 23 '14 at 11:25
  • So now I have this file with CRLF line endings. I opened the file and saved it again with only LF line endings (I checked it in a hex editor). Git diff shows that all lines are altered. Git add and Git commit result in having all lines replaced. – William van Veldhoven Aug 23 '14 at 11:29

1 Answers1

2

From what I remember of the documentation, you can't do precisely that.

However, the configuration let you ignore specific line endings.

If you want to store in the repository all files using LF line endings, run:

git config --system core.autocrlf false git config --system core.eol lf

Also you may want to ensure that the global, system and local configs are set to what you are trying to achieve:

git config --global -l git config --system -l git config --local -l

pyb
  • 4,813
  • 2
  • 27
  • 45