Git .gitattributes
In this page:
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
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
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
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
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
echo "node_modules/" >> .gitignore
echo "*.psd binary" >> .gitattributes
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: