Understanding End of Line in Git

Using .gitattributes fixes the problem where developers work cross platform on the same files and end up with end of line conflicts or file changes.

By using .gitattributes file, we can dictate how git treats each file type during commits and checkouts.

The git config --global core.eol setting by default should be empty or 'native'. This means that by default when Git writes files, it will convert them to your sytem as the native format.

During a commit, the default behavior for each file type defined as 'text' is convert it to 'lf' line ending, do the merge, then store in Git.

During a checkout, the default behavior for each file type defined as 'text' is to convert it back to the files native format based upon the OS.

If there are files that we always what to be converted to a certain type, no matter the OS, then eol=lf or eol=crlf.

Fixing End of Line

There are two methods for using gitattributes: creating a .gitattributes file, or using global config. Ideally you use the .gitattributes file because it doesn't require anyone on your team to do anything.

Create a file at the root of our repo

touch .gitattributes

Everything in this file takes the format

pattern attr1 attr2 attr3 etc

Ex:

*.cs text

This lets git know it should treat a .cs file like a text file the alternative being binary, for which line endings should not be applied.

In this example, the text attribute lets git know the files is a text file.

However if you want to guarantee that while your working on a file (your Git working tree), that it has the Windows line ending crlf, an example of this being a Visual Studio .sln file, then you would do something like:

*.sln eol=crlf

With this attribute, Git will ensure that *.sln files are normalized, but when in the working tree it will be converted to crlf so your Visual Studio editor won't complain about it.

.gitattributes

#Default all files it determins as text, normalize them.
* text=auto eol=lf

# These files are text and should be normalized (convert crlf => lf)
# Git Files
.gitattributes text eol=lf
.gitignore text eol=lf
.gitconfig text eol=lf

# cSharp / Visual Studio

*.bat     text eol=crlf
*.cmd     text eol=crlf
*.cs      text eol=crlf diff=csharp 
*.csproj  text eol=crlf
*.h       text eol=crlf
*.md      text eol=crlf
*.msbuild text eol=crlf
*.ps1     text eol=crlf
*.sdf     binary
*.sln     text eol=crlf
*.tt      text eol=crlf
*.xaml    text eol=crlf

# Web
*.css     text eol=lf
*.map     text eol=lf
*.sass    text eol=lf
*.json    text eol=lf
*.js      text eol=lf
*.htm     text eol=lf
*.chm     binary
*.html    text eol=lf
*.cshtml  text eol=lf
*.xml     text eol=lf
*.svg     text eol=lf

# Images & Media
*.png     binary
*.jpg     binary
*.jpeg    binary
*.gif     binary
*.ico     binary
*.mov     binary
*.ico     binary
*.pdf     binary


# Compression
*.gz      binary
*.zip     binary
*.7z      binary
*.nupkg   binary

# Fonts
*.ttf       binary
*.eot       binary
*.woff      binary

# Config
*.conf text eol=lf

# Documents
*.doc  diff=astextplain 
*.DOC  diff=astextplain
*.docx diff=astextplain
*.DOCX diff=astextplain
*.dot  diff=astextplain
*.DOT  diff=astextplain
*.pdf  diff=astextplain
*.PDF  diff=astextplain
*.rtf  diff=astextplain
*.RTF  diff=astextplain


*.txt text eol=lf
*.md text eol=lf
*.adoc text eol=lf
*.textile text eol=lf
*.csv text eol=lf
*.tab text eol=lf
*.tsv text eol=lf
*.sql text eol=lf
*.psd binary
*.ai binary
*.eps binary

# Compiled Dynamic libraries
*.so      binary
*.dylib   binary
*.dll     binary
*.pdb     binary

# Compiled Static libraries
*.lai   binary
*.la    binary
*.a     binary
*.lib   binary
*.llblgenproj binary

# Executables
*.exe binary
*.out binary
*.app binary

eof without a .gitattributes file

If you don't provide a .gitattributes file, then Git will fallback to the global git config --global core.autocrlf value.

Nicer Diffs with custom or default diff drivers

You can also add an attribute of diff=[driver] where driver is either a built in driver such as csharp or you can create custom drivers.

Ex: *.cs text diff=csharp

More build in drivers

References



blog comments powered by Disqus

Published

21 October 2015

Categories

Tags