rodrigosantosbr

[Git] Line Endings

Dec 30th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!

Summary

If you’re on a team of Windows developers - or more importantly, on a cross-platform development team - one of the things that comes up constantly is line endings.

The key to dealing with line endings is to make sure your configuration is committed to the repository, using .gitattributes.
For most people, this is as simple as creating a file named .gitattributes at the root of your repository that contains one line:

* text=auto
*.png -text
*.jpg -text
*.bmp -text
*.gif -text

With this set, Windows users will have text files converted from Windows style line endings (\r\n) to Unix style line endings (\n) when they’re added to the repository.

You can remove the text attribute from files that you don't want to have line ending conversions.
For example, if you have PNGs in your repository, your .gitattributes might look like this:

* text=auto
*.png -text

Why not core.autocrlf?

Originally, Git for Windows introduced a different approach for line endings that you may have seen: core.autocrlf.

git config --global core.autocrlf true

This is a similar approach to the attributes mechanism: the idea is that a Windows user will set a Git configuration option core.autocrlf=true and their line endings will be converted to Unix style line endings when they add files to the repository.

The difference between these two options is subtle, but critical: the .gitattributes is set in the repository, so its shared with everybody. But core.autocrlf is set in the local Git configuration. That means that everybody has to remember to set it, and set it identically.

The problem with core.autocrlf is that if some people have it set to true and some don’t, you’ll get a mix of line endings in your repository. And that’s not good - because his setting doesn’t just tell Git what you want it to do with files going in to your repository. It also tells Git what you’ve already done, and what the line endings look like on the files that are already checked in.

This is why one of the most common symptoms of a line ending configuration problem is seeing “phantom changes”: running git status tells you that you’ve changed a file, but running git diff doesn’t show you any changes. How can that be? Line endings.

source: http://www.edwardthomson.com/blog/git_for_windows_line_endings.html

Add Comment
Please, Sign In to add comment