Sub topic 9
Where attributes live and which rule wins
This chapter assumes you can write basic .gitattributes rules (text=auto, eol=lf, binary) and run git check-attr. It covers where Git looks for attributes and how it resolves conflicts between them.
Five places Git reads attributes
Git can read attributes from several files. When the same attribute is set in more than one of them, the file with the higher precedence wins.
| Precedence | File | Scope |
|---|---|---|
| 1 (highest) | `.git/info/attributes` | This clone only, never committed |
| 2 | `.gitattributes` in the file's own directory | Shared through the repository |
| 3 | `.gitattributes` in each parent directory, up to the top of the working tree | Shared through the repository |
| 4 | The file named by `core.attributesFile` (default `~/.config/git/attributes`) | All your repositories |
| 5 (lowest) | The system-wide `gitattributes` file in Git's `etc` directory | Every user on the machine |
Among the committed files, the one closest to the path wins. A .gitattributes in tools/ overrides the one in the repository root for files under tools/.
Use .git/info/attributes for local experiments. Put team rules in a committed .gitattributes. Put personal defaults in the global file.
Rules inside one file
Patterns follow .gitignore syntax with two differences. Negated patterns (!*.txt) are not allowed, and a pattern in a nested file is relative to that file's directory. A pattern with no slash, such as *.sh, matches at any depth below the file that contains it.
Within a single file, the last matching line wins. Put general rules first and exceptions after them:
* text=auto
*.sh text eol=lf
*.png binaryResolution works per attribute, not per line. If one rule sets text and a later one sets only eol, the path gets both. A later rule replaces only the attributes it names.
The four attribute states
Every attribute on a path is in one of four states.
| Written as | State | Meaning |
|---|---|---|
| `text` | set | Treat as text |
| `-text` | unset | Never treat as text |
| `text=auto` | value | Git detects whether the file is text |
| `!text` | unspecified | Forget earlier rules, fall back to defaults |
!text is the only way to cancel an earlier rule and return to the default. It is how a subdirectory opts out of a root-level rule.
Macros expand in place
binary is a macro. It expands to -diff -merge -text. Because it is shorthand, later lines can override its parts:
*.dat binary
*.dat diffHere *.dat files stay unset for text and merge, but diff is set again by the later line.
Attributes versus core.autocrlf
Attributes beat configuration. If text or eol is set for a path, Git uses that rule and ignores core.autocrlf. Git falls back to core.autocrlf and core.eol only when text is unspecified. This is why a committed .gitattributes gives the same result on every machine, whatever each developer's personal settings.
Worked example
The script below builds a small repository with a root rule, a nested override and a local override. It then checks the result after each step.
git init demo && cd demo
mkdir tools
touch tools/setup.sh
printf '* text=auto\n*.sh text eol=lf\n' > .gitattributes
printf '*.sh eol=crlf\n' > tools/.gitattributes
git check-attr text eol -- tools/setup.sh
# tools/setup.sh: text: set
# tools/setup.sh: eol: crlf
echo '*.sh -text' >> .git/info/attributes
git check-attr text eol -- tools/setup.sh
# tools/setup.sh: text: unset
# tools/setup.sh: eol: crlfIn the first check, text comes from the root file and eol comes from tools/.gitattributes, so each attribute is resolved separately. The line in .git/info/attributes then overrides text for this clone only. Git does not convert a path whose text is unset, so the remaining eol=crlf has no effect. Nothing in the repository shows this override, so keep .git/info/attributes in mind when two clones behave differently.
Debugging a conflict
When a file is treated differently than you expect, work through these steps:
- Run
git check-attr -a -- path/to/fileto see the final result. - Check
.git/info/attributesandgit config core.attributesFilefor hidden overrides. - Look for a
.gitattributesin the file's directory and its parents. - Read each file from the bottom up, because the last match wins.
Exercise
The root .gitattributes contains *.sh text eol=lf. A file ci/.gitattributes contains *.sh eol=crlf. Your .git/info/attributes is empty. What does git check-attr text eol -- ci/run.sh report?
Show answer
text is set and eol is crlf. The nested file is closer to the path, so its eol value replaces the root value. It does not mention text, so text still comes from the root rule.
Exercise
How would a subdirectory vendor/ opt out of a root rule * text=auto without declaring its files binary?
Show answer
Add * !text to vendor/.gitattributes. This returns text to the unspecified state, so Git falls back to core.autocrlf and core.eol. Writing * -text would also stop conversion, but it marks the files as not text instead of leaving them unspecified.