Skip to content
Tutorials Center
My learning

Topics

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.

GITATTRIBUTES
* text=auto
*.sh text eol=lf
*.png binary
docs/** linguist-documentation

Try it

Bash Setting attributes and querying them

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.

Bash Attributes in effect: -diff and export-ignore

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

GITATTRIBUTES
<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/attributes is used, since it has no working tree.
  • Line endings: * text=auto lets Git detect text files and normalize them; use eol=lf or eol=crlf to force a working-tree ending. See text, eol and crlf attributes.
  • Diff and merge control: diff=<driver> picks a textconv or word-level diff, -diff turns off textual diffs, and merge=<driver> or merge=binary customizes merges; see diff and merge attributes.
  • Filters such as LFS use filter=lfs; see filter attribute.
  • Export control: export-ignore and export-subst change what git archive produces; see export-ignore and export-subst.
  • Other attributes include ident (expand $Id$), working-tree-encoding, whitespace (per-path core.whitespace), delta, conflict-marker-size and linguist-* hints used by hosting sites.
  • Changes need a re-checkout to apply to files already in the working tree. After changing text or eol, run git add --renormalize . to record the converted content.
  • Inspect with git check-attr (see git check-attr); -a lists every attribute that applies to a path, --all is the long form, and --cached reads from the index instead of the working tree.
  • Pattern gotchas: *.png matches at any depth, /logo.png only at the file's level, and a pattern containing a slash is anchored. Unlike .gitignore, directories are not expanded: use docs/** to apply an attribute to every file below.