patterngitModerate
.gitattributes: control line endings, diffs, and merge strategies
Viewed 0 times
gitattributesline endingsCRLF LFbinary filesmerge strategyeol
Error Messages
Problem
Cross-platform teams get noisy diffs due to line ending differences (CRLF vs LF), or binary files are diffed as text, producing unreadable output. Generated files cause merge conflicts that should always be regenerated.
Solution
Use .gitattributes to normalize behavior:
# .gitattributes
# Force LF for all text files
* text=auto eol=lf
# Treat specific files as binary
*.png binary
*.jpg binary
*.ico binary
# Always use ours strategy for generated lockfiles
package-lock.json merge=ours
# Improve diff readability for JavaScript functions
*.js diff=javascript
# .gitattributes
# Force LF for all text files
* text=auto eol=lf
# Treat specific files as binary
*.png binary
*.jpg binary
*.ico binary
# Always use ours strategy for generated lockfiles
package-lock.json merge=ours
# Improve diff readability for JavaScript functions
*.js diff=javascript
Why
.gitattributes is committed to the repo and applies uniformly to all clones. It overrides local git config for the affected paths, ensuring consistent behavior across Windows, macOS, and Linux.
Gotchas
- After adding .gitattributes, re-normalize existing files:
git add --renormalize . - The
merge=oursstrategy makes every conflict always pick your version — ensure this is intentional for the file type - Binary detection is heuristic — explicitly marking files as binary prevents false-positive text normalization
Code Snippets
Recommended .gitattributes for a cross-platform web project
# .gitattributes
* text=auto eol=lf
*.sh eol=lf
*.bat eol=crlf
*.png binary
*.jpg binary
*.gif binary
*.ico binary
*.woff2 binary
package-lock.json merge=ours
yarn.lock merge=ours
*.js diff=javascript
*.ts diff=javascriptContext
Projects with cross-platform developers or with generated files that shouldn't be manually merged
Revisions (0)
No revisions yet.