← Back to Git Course | Chapter 10: Tools & Internals | Lesson 7 of 8

Git .gitattributes

A .gitattributes file is like a set of special instructions on file labels, telling Git how to handle certain files, such as which are pictures and not text.
Syntax
bash
# .gitattributes
pattern attribute
* text=auto
*.extension binary

What is .gitattributes

A .gitattributes file lets you tell Git how to treat specific files or patterns -- line-ending normalization, marking a file as binary, or choosing a diff/merge strategy -- rules that apply to everyone who clones the repository, unlike personal Git config.

Example: What is .gitattributes

bash
echo "*.psd binary" > .gitattributes

Normalizing Line Endings

Windows and Unix-based systems use different line-ending characters (CRLF vs LF); without normalization, a repository shared across both can show every line of a file as changed purely due to line-ending differences, cluttering diffs with noise.

Example: Normalizing Line Endings

bash
echo "* text=auto" >> .gitattributes

Marking Files as Binary

Explicitly marking a file type as binary prevents Git from trying to merge or diff it as text, which for genuinely binary formats like images or compiled archives would otherwise produce meaningless or broken output.

Example: Marking Files as Binary

bash
echo "*.png binary" >> .gitattributes

Custom Diff and Merge Drivers

For file types Git can't meaningfully diff out of the box -- like Jupyter notebooks or Word documents -- .gitattributes can point to a custom diff or merge driver script that understands that format's structure.

Example: Custom Diff and Merge Drivers

bash
echo "*.ipynb diff=jupyternotebook" >> .gitattributes

.gitattributes vs .gitignore

The two files solve different problems: .gitignore tells Git which files to never track at all, while .gitattributes changes how Git treats files it IS tracking -- line endings, diff behavior, export handling -- they're complementary, not alternatives.

Example: .gitattributes vs .gitignore

bash
echo "node_modules/" >> .gitignore
echo "*.psd binary" >> .gitattributes
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Adding * text=auto to .gitattributes and not running git add --renormalize ., so files keep their old line endings.
  2. Not marking files as binary, so Git tries to diff or merge them as text.
  3. Naming the file .gitattribute or gitattributes, so Git does not read it; the name must be .gitattributes.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.