.gitattributes file allows you to specify attributes for paths in your repository. Attributes control various Git behaviors like line ending normalization, diff generation, merge strategies, and more.
Overview
Git attributes provide fine-grained control over how Git handles specific files. Common use cases include:- Line ending normalization (CRLF vs LF)
- Custom diff and merge drivers
- Binary file handling
- Export behavior for archives
- Language-specific diff patterns
File Format
Each line in.gitattributes follows this format:
Example
File Locations
Repository .gitattributes
Location: Root or any directory in the repositoryScope: Shared with all users via version control
Per-Repository Attributes
Location:.git/info/attributesScope: Local to your repository, not shared
Global Attributes
Location: Configured viacore.attributesFileScope: Applies to all your repositories
Pattern Matching
Patterns use the same syntax as.gitignore:
Pattern Examples
Attribute States
- Set:
text- Attribute is enabled - Unset:
-text- Attribute is disabled - Set to value:
eol=lf- Attribute has specific value - Unspecified: Attribute not mentioned
Line Ending Handling
text
Controls line ending normalization:text (set)
text (set)
Enable line ending normalization. Convert to LF in repository, convert on checkout based on
core.eol or eol attribute.text=auto
text=auto
Let Git automatically detect text files and normalize line endings.This is the recommended setting for most repositories.
-text
-text
Disable line ending normalization. Files are stored and checked out as-is.
eol
Specify line ending style in working directory:eol=lf- Use LF (Unix-style) line endingseol=crlf- Use CRLF (Windows-style) line endings
Complete Line Ending Strategy
Binary Files
Thebinary macro is equivalent to -text -diff:
Common Binary Patterns
Diff Attributes
diff
Control how Git generates diffs:diff (set)
diff (set)
Treat as text and generate normal diffs.
-diff
-diff
Treat as binary. Shows “Binary files differ” instead of diff.
diff=<driver>
diff=<driver>
Use custom diff driver.
Built-in Diff Patterns
Git includes built-in diff patterns for many languages:Merge Attributes
merge
Control merge behavior:merge (set)
merge (set)
Use standard 3-way merge.
-merge
-merge
Don’t merge, take version from current branch and mark as conflict.
merge=<driver>
merge=<driver>
Use custom merge driver.
Built-in Merge Drivers
Filter Attributes
filter
Apply filters when checking in/out files:.git/config:
Git LFS Integration
Git Large File Storage (LFS) uses attributes to track large files:git lfs track:
Export Attributes
export-ignore
Exclude files fromgit archive:
export-subst
Expand placeholders in exported files:version.txt:
$Format:...$ with actual values.
Common Configuration Examples
Basic Web Project
Cross-Platform Project
Project with LFS
Unity Game Project
Working with Attributes
Check Attributes for a File
Apply Attributes to Existing Files
After adding or changing.gitattributes, normalize existing files:
Best Practices
Start with text=auto
Use
* text=auto as the first line to auto-detect text filesBe Explicit for Scripts
Always specify
eol=lf for shell scripts and eol=crlf for Windows scriptsMark Binary Files
Explicitly mark binary files to prevent corruption
Commit .gitattributes
Always commit
.gitattributes to share settings with your teamRecommended Template
Troubleshooting
Line Endings Still Wrong
Problem: Line endings aren’t being normalized Solution: Re-normalize the repository:Binary Files Showing Diffs
Problem: Binary files showing garbled diffs Solution: Mark them as binary:Check Current Attributes
See Also
- Configuration Overview - Git configuration system
- .gitignore - Ignore patterns for untracked files
- .gitconfig Reference - Git configuration variables
