Q23 of 40 · Git
What is `.gitattributes` and how do you use it to prevent line-ending and diff issues in a cross-platform QA team?
Short answer
Short answer: `.gitattributes` declares per-path attributes that control line endings (`text eol=lf`), diff drivers, merge strategies, and binary file handling. It solves the Windows CRLF vs Unix LF problem at the repo level rather than relying on per-developer config.
Detail
.gitattributes is a repo-level configuration file (committed to the repository) that tells Git how to treat specific file patterns. The most common QA use case is fixing line-ending chaos when the team mixes Windows and Mac/Linux machines.
Line endings: setting * text=auto lets Git normalise all text files to LF in the repository and convert to the platform's native ending on checkout. For critical files (shell scripts, Dockerfiles) you can force LF explicitly: *.sh text eol=lf. For binary files like screenshots or keystores, mark them binary to prevent Git from touching them at all.
Diff drivers: you can tell Git to use a custom diff driver for specific file types. For example, marking *.pdf binary stops Git generating useless binary diffs. You can also configure linguist-language attributes for GitHub to correctly syntax-highlight Karate .feature files.
Merge strategy: the merge=ours attribute tells Git to always keep your side during a merge conflict for auto-generated files (e.g., lock files you regenerate during CI).
Export-ignore: paths marked export-ignore are excluded from git archive exports — useful for keeping test fixtures out of release tarballs.
// EXAMPLE
# .gitattributes (committed at repo root)
# Normalize all text files to LF in the repo
* text=auto eol=lf
# Force LF for scripts regardless of OS
*.sh text eol=lf
*.bash text eol=lf
# Force CRLF for Windows-only files
*.bat text eol=crlf
*.cmd text eol=crlf
# Binary files — no line-ending conversion, no text diffs
*.png binary
*.jpg binary
*.pdf binary
*.zip binary
*.jks binary
# Karate feature files — treat as text (default), hint GitHub language
*.feature text linguist-language=Gherkin
# Auto-generated lock files — keep ours during merge conflicts
package-lock.json merge=ours
# Exclude from git archive (release tarballs)
test/fixtures/ export-ignore
.github/ export-ignore