Git Reference ยท Files and Formats
The .gitattributes file
Reference for .gitattributes: pattern and attribute syntax, set/unset/value states, macro attributes, file locations and precedence, and how to check them.
A .gitattributes file assigns attributes to paths. An attribute is a named setting that other parts of Git read when they handle a file: text and eol for line endings, diff and merge to choose drivers, filter for clean and smudge programs, export-ignore for git archive, linguist-* for hosting sites, and many more. The file itself does nothing; it only labels paths.
Each line has a pattern followed by attributes separated by spaces. The pattern follows the .gitignore style, with a few differences (no negation, no directory-only rule). Because the file is committed, every clone gets the same behavior, which is why it is the right place for line-ending policy and binary-file markers.
* text=auto *.sh text eol=lf *.png binary docs/** linguist-documentation
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
<pattern> <attr> [<attr>...] [attr]<name> <attr>... # define a macro attribute
| Attribute form | Meaning |
|---|---|
name |
Set: the attribute is on. |
-name |
Unset: explicitly off. |
name=value |
Value: set to a string. |
!name |
Unspecified: discard any earlier rule for this attribute. |
Built-in macro: binary is shorthand for -diff -merge -text.
Files and precedence (highest first): $GIT_DIR/info/attributes; .gitattributes in the same directory as the path and then each parent directory up to the root (deeper wins); core.attributesFile (default ~/.config/git/attributes); the system-wide file. In a line, the last matching pattern overrides earlier ones for the same attribute.
Errors and edge cases: an attribute name may not start with - or ! and may only use letters, digits, -, _ and .. Patterns follow the gitignore rules, but a trailing / does not make the rule directory-only and !pattern does not negate. Attributes are read from the working tree by default; with git archive or --attr-source, they come from the commit instead.
Usage notes
- Commit the file before relying on it. For a bare repository,
info/attributesis used, since it has no working tree. - Line endings:
* text=autolets Git detect text files and normalize them; useeol=lforeol=crlfto force a working-tree ending. See text, eol and crlf attributes. - Diff and merge control:
diff=<driver>picks a textconv or word-level diff,-diffturns off textual diffs, andmerge=<driver>ormerge=binarycustomizes merges; see diff and merge attributes. - Filters such as LFS use
filter=lfs; see filter attribute. - Export control:
export-ignoreandexport-substchange whatgit archiveproduces; see export-ignore and export-subst. - Other attributes include
ident(expand$Id$),working-tree-encoding,whitespace(per-pathcore.whitespace),delta,conflict-marker-sizeandlinguist-*hints used by hosting sites. - Changes need a re-checkout to apply to files already in the working tree. After changing
textoreol, rungit add --renormalize .to record the converted content. - Inspect with
git check-attr(see git check-attr);-alists every attribute that applies to a path,--allis the long form, and--cachedreads from the index instead of the working tree. - Pattern gotchas:
*.pngmatches at any depth,/logo.pngonly at the file's level, and a pattern containing a slash is anchored. Unlike.gitignore, directories are not expanded: usedocs/**to apply an attribute to every file below.