buf0rq
Last Updated: December 29, 2017
·
34.08K
· ninjascribble

Use .gitattributes to normalize line-endings

The .gitattributes file can override configuration for specific files or paths. In effect, this means that you can check project-specific configuration directly into your git repo, and be assured that every developer will be working with roughly the same configuration options.

This came in especially handy on a recent project. Whenever a certain team member would check in a change, no matter how small, the diff would be enormous! It didn't take long to figure out that we were using different line-endings, and that git interpreted that every single line had been changed. But there was no warning, and by the time we realized it the damage had already been done.

By adding a .gitattributes file to every project since, we've avoided the issue entirely. Developers don't have to change their setup to start working on a project, and our git history remains pristine. Hooray!

# Declare files that will always have CRLF line endings on checkout.
*.css text eol=crlf
*.html text eol=crlf
*.js text eol=crlf

4 Responses
Add your response

6333

that can also be sorted out in a broader way using the core.autocrlf setting.

over 1 year ago ·
6337

@etienne That's true, but core.autocrlf is a user-controlled git config setting. The nice thing about using a .gitattributes file is that it applies project-specific overrides to a user's personal config settings so that your teammates don't have to change their config preferences just to work on your project.

over 1 year ago ·
6360

but it also means that the line endings might not be correct for the platform. In your example forcing Windows CRLF endings could cause issues in Linux and OSX.

A better suggestion is to normalize the files upon commit and checkout.

- Set your global core.autocrlf to input for linux/OSX or true for Windows

- Add "* text" in the .gitattributes file

When committed they are stored with LF, on checkout they are converted to the OS's native line endings.

For more about this, read https://help.github.com/articles/dealing-with-line-endings

It also has a tip on how to re-normalize the entire repo if you are ever in the situation again where the developers have been using different line endings.

Another great blog about line endings with git is http://timclem.wordpress.com/2012/03/01/mind-the-end-of-your-line/

over 1 year ago ·
27913

@insomniacsoftware - .gitattributes can override it all and ensure line endings are correct for the platform, committed how you want, and configurable per file type if you like. It's all mentioned in the first link you provide. Point is, you don't have to worry about whether the user has set global core.autocrlf. The global setting can also mess up repositories that should be CRLF.

over 1 year ago ·