Sub topic 11
Related attributes: diff, merge and export-ignore
This chapter assumes you can already write a .gitattributes file with text, eol and binary, and that you know the basic Git workflow. Line endings are only one use of attributes. The same file can also change how Git diffs, merges and exports files.
What these attributes control
Git consults attributes at specific moments. Each attribute below affects one of them.
| Attribute | When Git reads it | Effect |
|---|---|---|
| `diff` | `git diff`, `git log -p`, `git show` | Chooses the diff driver, or turns text diffs off |
| `merge` | `git merge`, `git pull`, `git rebase` | Chooses the merge driver |
| `export-ignore` | `git archive` | Leaves the path out of the archive |
None of these change what is stored in the repository. They change how Git presents or combines content.
Merge behavior
By default Git merges text files line by line and reports a conflict when both sides changed the same region. The merge attribute changes this for chosen paths.
# Keep both sides when merging a changelog
CHANGELOG.md merge=union
# Treat a database as binary and never merge it as text
*.db binary merge=binaryThe built-in drivers are:
merge=unionkeeps the lines from both sides and leaves no conflict markers. It does not reorder or deduplicate lines, so use it only for append-style files such as changelogs.merge=binary(or thebinarymacro) never attempts a textual merge. If both sides changed the file, Git reports a conflict and you pick one version.
The binary macro already sets -diff -merge -text, so *.db binary alone stops text merging. Adding merge=binary is explicit but redundant in that case.
Diff behavior
The diff attribute has two common uses.
-diffstops Git from printing a textual diff. Git showsBinary files differinstead. This is useful for large generated files such as minified bundles or lock files that nobody reviews line by line.diff=<name>selects a named driver. Git ships built-in funcname patterns for several languages, for example*.py diff=pythonor*.md diff=markdown. These improve the hunk header that shows the enclosing function or section.
# Do not print line-by-line diffs for generated output
*.min.js -diff
package-lock.json -diff
# Use language-aware hunk headers
*.py diff=python
*.md diff=markdownYou can also define a custom driver in your Git config and refer to it by name. The textconv option converts a file to text before diffing. Driver definitions live in config, not in .gitattributes, so each developer must have them.
Keeping files out of archives
git archive builds a tarball or zip from a commit. Paths marked export-ignore are left out. This is how projects ship a release without tests, documentation sources or repository configuration.
docs/ export-ignore
tests/ export-ignore
.gitattributes export-ignoreTry it:
git archive --format=tar.gz --output=release.tar.gz HEAD
tar -tzf release.tar.gzThe listing should not contain docs/, tests/ or .gitattributes. The working tree and normal clones are not affected. Hosting services that generate "download ZIP" links usually use git archive, so they follow the same rules.
Common errors
- Expecting
export-ignoreto affect clones. It only affectsgit archive. Files remain in the repository. - Using
merge=unionon structured files. JSON or YAML can end up invalid, because both sides' lines are concatenated. - Assuming a custom driver travels with the repository. Attributes are committed, but the driver definition in config is not. A missing driver makes Git fall back to default behavior.
- Forgetting the pattern is relative to the
.gitattributeslocation. A trailing slash such asdocs/matches a directory, and a pattern without a slash matches at any depth. - Checking the wrong thing. Use
git check-attrto confirm what applies to a path.
git check-attr diff merge export-ignore -- CHANGELOG.md docs/index.mdEach line reports set, unset, unspecified or a value, which tells you whether your rule matched.
Exercise
A repository has a generated bundle.min.js that appears as thousands of changed lines in every pull request, and a tests/ directory that should not appear in release tarballs. Which two .gitattributes lines solve this, and which command lets you verify the second one?
Show answer
Add bundle.min.js -diff (or *.min.js -diff) so Git stops printing line diffs for it, and tests/ export-ignore so git archive omits the directory. Verify the second with git archive --format=tar HEAD | tar -t, or check the attribute resolution with git check-attr export-ignore -- tests/example_test.py.