← Back to Chapters

Git .gitattributes

? Git .gitattributes

? Quick Overview

The .gitattributes file is used to define attributes for paths in your Git repository. It controls how files behave during Git operations such as merging, diffing, and checking out.

? Key Concepts

  • Path-based attribute control
  • Line ending normalization
  • Binary file handling
  • Custom diff and merge strategies

? Syntax / Theory

The file contains path patterns followed by attribute rules.

? View Code Example
# Normalize line endings for all text files
* text=auto

# Markdown files with custom diff
*.md text diff=markdown

# Treat images as binary
*.jpg binary

▶️ Live Explanation

Git automatically converts line endings and avoids merging or diffing binary files, ensuring consistent behavior across platforms.

? Common Use Cases

Normalizing Line Endings

? View Code Example
# Automatically normalize line endings
* text=auto

Handling Binary Files

? View Code Example
# Mark image files as binary
*.png binary
*.jpg binary

Custom Merge Strategies

? View Code Example
# Prevent automatic merge for PNG files
*.png merge=binary

? Advanced Configuration

Custom Diff Drivers

? View Code Example
# Use markdown diff driver
*.md diff=markdown

Directory-Specific Rules

? View Code Example
# Apply rules only inside docs directory
docs/* text=auto

? Tips & Best Practices

  • Always commit .gitattributes early in the project to avoid line-ending conflicts later.
  • Use text=auto to keep repositories cross-platform friendly.
  • Mark binary files explicitly to prevent unnecessary merge conflicts.
  • Test attribute changes with git check-attr to verify behavior.

? Try It Yourself

  • Create a .gitattributes file and define rules for text and binary files.
  • Test line ending normalization on Windows and Linux systems.
  • Configure a custom diff driver for markdown files.