Sub topic 3
Sharing rules with a .gitattributes file
This chapter assumes you can commit and push, you know what LF and CRLF line endings are, and you have seen core.autocrlf. Here you learn what a .gitattributes file is and how to write its lines. Later chapters cover individual attributes in detail.
Why a file in the repository
core.autocrlf lives in each developer's own Git configuration. A new clone starts with no setting, and each person may choose differently. The result is a repository where the rules depend on who last touched a file.
A .gitattributes file is different. It is a normal tracked file. It travels with every clone, so every contributor on every operating system gets the same rules. Git applies these rules when it adds files to the index and when it writes files to the working tree.
The shape of a line
Each line has a path pattern followed by one or more attributes, separated by whitespace:
# pattern attributes
* text=auto
*.sh text eol=lf
*.png binaryLines starting with # are comments. An attribute can be in one of four states:
| Written as | State | Meaning |
|---|---|---|
| `text` | set | The attribute is on. |
| `-text` | unset | The attribute is explicitly off. |
| `text=auto` | value | The attribute has a specific value. |
| `!text` | unspecified | Cancels any earlier rule so Git falls back to its default. |
Telling "unset" apart from "unspecified" matters. -text forbids conversion. !text means no rule applies, so configuration such as core.autocrlf can still act.
Pattern syntax
Patterns look like .gitignore patterns, with a few differences.
- A pattern with no slash, such as
*.sh, matches that name at any depth. - A pattern with a slash, such as
/vendor/*.jsordocs/*.md, is relative to the directory that holds the.gitattributesfile. **matches across directories, sodocs/**/*.mdmatches Markdown files at any depth underdocs.- A pattern that names a directory does not apply to the files inside it. To cover everything under a directory, write
vendor/**. - You cannot negate a pattern with a leading
!. Use a more specific later line instead.
Which line wins
Git reads the file from top to bottom. For each attribute, the last matching line wins. Attributes that the later line does not mention keep their earlier values. This is why the broad rule comes first and the exceptions follow:
# Default: detect text files
* text=auto
# Exception: these must use LF in the working tree
*.sh text eol=lf
# Exception to the exception: vendored code stays untouched
vendor/** -textFor vendor/install.sh, text is unset by the last line. The eol=lf value from the middle line is still set, but it has no effect because the file is not treated as text.
Where to put the file
Put a single .gitattributes file at the repository root for most projects. Commit it like any other file:
git add .gitattributes
git commit -m "Add .gitattributes"Git also reads .gitattributes files in subdirectories. These apply to their own subtree, and their patterns are relative to that directory. For rules that should stay on your machine, .git/info/attributes exists, but nobody else receives it. A later chapter covers precedence between these locations.
Checking that a rule matches
After editing the file, confirm that a path picks up the attributes you expect:
git check-attr text eol -- scripts/deploy.shGit prints one line per attribute, such as scripts/deploy.sh: eol: lf. If it prints unspecified, no rule matched. Usually the pattern is wrong, or a directory pattern is missing /**.
Exercise
A repository has the lines * text=auto followed by docs/ -text. Files under docs/ are still being converted. Why, and what is the fix?
Show answer
A pattern that names a directory does not apply to the files inside it, so docs/ -text sets nothing on them. The * text=auto rule still applies to those files. Change the line to docs/** -text.
Exercise
In what order should you write a broad rule such as * text=auto and a narrow rule such as *.sh text eol=lf, and why?
Show answer
Write the broad rule first. Git lets the last matching line win for each attribute, so the narrow rule overrides or adds to the broad one for .sh files. Reversing the order would let * text=auto overwrite the text value set for .sh files.