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

Git .gitattributes

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

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.