What is the purpose of `text = auto` in the `.gitattributes` file?

Basically .gitattributes file has * text=auto . What is the purpose of text=auto in this file?

+109
git gitattributes
Jan 31 '14 at 5:22
source share
3 answers

This ensures the normalization of line endings. Source: Kernel.org

When the text is set to "auto", the path is marked to automatically normalize the end of the line. If git decides that the content is text, its line endings normalize to LF upon registration.

If you want to interact with a source control system that provides normalization of the end of the line, or you just want all the text files in your repository to be normalized, you should instead set the text attribute to โ€œautoโ€ for all files.

This ensures that all files that git considers to be text will have normalized (LF) line endings in the repository.

+56
Jan 31 '14 at 5:31
source share

From the docs :

Each line in the .gitattributes file (or .git/info/attributes ) has the form:

 pattern attr1 attr2 ... 

So, the template is * , which means all files, and the attribute is text=auto .

What does text=auto do? From the documentation:

When the text is set to "auto", the path is specified to automatically normalize the final line. If Git decides that the content is text, its line ending is normalized to LF upon registration.

What is the default behavior if it is not enabled?

Not chosen

If no text attribute is specified, Git uses the core.autocrlf configuration variable to determine if the file should be converted.

What does core.autocrlf do? From the docs:

  core.autocrlf 

Setting this variable to โ€œtrueโ€ is almost the same as setting the text attribute to โ€œautoโ€ for all files, except that text files are not guaranteed normalized: files containing CRLF in the repository will not be affected. Use this option if you want CRLF lines to end in your workspace although the repository does not have normalized line endings. This variable can be set to input, and in this case, the output conversion is not performed.

If you think all this is as clear as dirt, you are not alone.

Here, what * text=auto does in my words: when someone commits a file, Git guesses whether this file is a text file or not, and if so, it will commit a version of the file where all CR + LF bytes are replaced by bytes Lf. This does not affect what files look in the working tree, there are other settings that will convert LF bytes to CR + LF bytes when checking a file.

Recommendation:

I would not recommend putting * text=auto in a .gitattributes file. Instead, I would recommend something like this:

 *.txt text *.html text *.css text *.js text 

This explicitly indicates which files are text files that convert CRLF in LF to an object database (but not necessarily in the working tree). We had a repo with * text=auto , and Git guessed that it was a text file for the image file, which caused it to be damaged, because it replaced L + LF bytes with LF bytes in the object database. It was not fun to debug.

If you must use * text=auto , put it as the first line in .gitattributes so that later lines can override it. This seems to be an increasingly popular practice.

+55
Jun 24 '16 at 16:08
source share

This configuration relates to how line endings are processed. When enabled, all line ends are converted to LF in the repository. There are other flags regarding how line ends are converted to your working directory. Full information about us here: https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html

+7
Jan 31 '14 at 5:29
source share



All Articles