Confused line endings in R

Hello, I’m having an issue where every time a collaborator makes changes to an R file, it seems as if the whole file has changed. I think this is because he is working off a Windows while I’m on MacOS. I created the following gitattributes file and put it in the root directory for the project:

# Set the default behavior, in case people don't have core.autocrlf set.
* text=auto

# Declare files that will always have LF line endings on checkout.
*.R text eol=crlf

However, it doesn’t seem to be working. I upload by syncing with Github desktop and he uploads by downloading/uploading modified files via (since he can’t access the Github desktop application).

Any advice would be much appreciated!

GitHub’s WebUI is running on a Linux machine, so any text edits will use the native Unix EOL (LF).

The .giattributes settings you’ve defined control the rules for EOL normalization, but whether these are going to prevent commits that violate the rules depends on the Git settings on the end user machine.

I’m not sure whether the GH WebUI can solve this problem for you, but there are a few things you might attempt in this respects.

Pre-Commit Hooks

You can add to your repository a pre-commit hook that would execute at every commit a custom script that forces CRLF EOL normalization on all *.R files. Since this is a repository settings, even the GH WebUI should honor it.

In this case, all your hook would have to do is execute unix2dos on every *.R file in the repository.

NOTE — in theory, you could invoke Git to filter down the list to only files which are actually staged, but probably this could actually result in slower execution than just normalizing every file at each commit (at least, this was my general experience with most repositories, except a few rare cases where ignored files matching the pattern were in the order of hundreds of files that would be unnecessarily processed by the hook).

This solution should be bullet-proof.


You should also consider adding an .editorconfig file with an EOL rule for R files:

end_of_line = crlf

GitHub supports EditorConfig, so chances are that this rule might be enforced on WebUI commits too (but I’m not sure about this).

Having an .editorconfig settings file is always a good thing.


See also: