Skip to content

Git attributes and filter drivers quick reference

One-page reference for configuring .gitattributes, custom diff drivers (textconv), clean/smudge pipelines, and line ending normalizations.


1. Quick configuration recipes

Canonical JSON and YAML diffs

# In .gitattributes
*.json diff=json
*.yaml diff=yaml
*.yml diff=yaml

# In local or global Git config
git config diff.json.textconv "jq --sort-keys ."
git config diff.yaml.textconv "python3 -c 'import sys, yaml; print(yaml.dump(yaml.safe_load(sys.stdin.read()), sort_keys=True, indent=2))'"

Infrastructure hunk headers (Terraform and Kubernetes)

# In .gitattributes
*.tf diff=tf
*.tfvars diff=tf
*.k8s.yaml diff=k8s

# In Git config
git config diff.tf.xfuncname '^[[:space:]]*((resource|data|module|variable|output)[[:space:]]+"[^"]+"([[:space:]]+"[^"]+")?)'
git config diff.k8s.xfuncname '^([A-Za-z]+:.*|  name:.*)'

Clean and smudge secret redaction

# In .gitattributes
config/*.env filter=scrub-secrets

# In Git config
git config filter.scrub-secrets.clean "sed -E 's/(API_KEY|TOKEN)=.*/\1=REDACTED/'"
git config filter.scrub-secrets.smudge "cat"
git config filter.scrub-secrets.required false

2. Common command matrix

Command Purpose
git check-attr -a -- <file> List all resolved attributes for a specific file
git check-attr diff -- <file> Inspect assigned diff driver for a path
git check-attr filter -- <file> Inspect assigned clean/smudge filter for a path
git diff --textconv Generate diff using configured textconv drivers
git diff --no-textconv Bypass textconv and show raw file diff
git add --renormalize . Re-evaluate line endings and attributes across working tree
git config --get-regexp '^filter\.' List all registered filter drivers
git config --get-regexp '^diff\.' List all registered diff drivers

# Default line ending handling
* text=auto eol=lf

# Explicit source text formats
*.sh text eol=lf
*.py text eol=lf
*.js text eol=lf
*.ts text eol=lf
*.json text diff=json
*.yaml text diff=yaml
*.yml text diff=yaml
*.md text

# Infrastructure as code
*.tf text diff=tf
*.tfvars text diff=tf

# Explicit binary formats (no line ending translation, no text diffs)
*.png binary
*.jpg binary
*.tar.gz binary
*.tgz binary
*.zip binary
*.parquet binary
*.wasm binary
*.so binary

4. Verification

# Test attribute resolution
git check-attr -a -- deploy/app.tf
# deploy/app.tf: text: set
# deploy/app.tf: diff: tf

# Test clean filter execution manually on stdin
echo "API_KEY=super_secret_token_12345" | eval "$(git config filter.scrub-secrets.clean)"
# Output: API_KEY=REDACTED