Skip to content
Git Basics: Inspect Effective Attributes with check-attr

Git Basics: Inspect Effective Attributes with check-attr

October 27, 2026

A path can inherit Git attributes from several rules. git check-attr shows the effective values for a specific path. Compare the working attributes file with the staged version before changing a repository-wide text policy.

Prepare a scratch directory

Tested on Linux with Git 2.52.0 and GNU Bash 5.2.37. You need the named runtime, Bash, a text editor, and the standard mktemp, rm, and rmdir utilities. The script creates a second temporary fixture directory and removes only that directory through its exit trap. Run it in a normal Bash terminal, outside any Git hook or Git wrapper. No global Git configuration is changed.

Keep using the same terminal. Stop if setup fails, before saving the source:

start_dir="$PWD"
work_dir="$(mktemp -d)" || exit 1
cd -- "$work_dir" || exit 1

Run the example

Save this as demonstrate-check-attr.sh:

#!/usr/bin/env bash
set -euo pipefail
export LC_ALL=C
fixture_dir="$(mktemp -d)"
trap 'rm -rf -- "$fixture_dir"' EXIT
export GIT_CONFIG_NOSYSTEM=1
export GIT_CONFIG_GLOBAL=/dev/null
unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
git init -q -b main "$fixture_dir/repo"
cd -- "$fixture_dir/repo"
git config user.name 'Practice Author'
git config user.email 'practice@example.invalid'
printf '*.txt text eol=lf\n*.bin -text\nnotes.txt !text\n' > .gitattributes
printf 'note\n' > notes.txt
git add .gitattributes notes.txt
git commit -qm 'Attribute rules'
printf 'Working rules:\n'
git check-attr text eol -- report.txt notes.txt image.bin other.dat
printf '*.txt -text\n' > .gitattributes
printf 'Edited working rule:\n'
git check-attr text -- report.txt
printf 'Staged rule:\n'
git check-attr --cached text -- report.txt
test "$(git check-attr --cached text -- report.txt)" = 'report.txt: text: set'

Run it:

bash demonstrate-check-attr.sh

Observed output:

Working rules:
report.txt: text: set
report.txt: eol: lf
notes.txt: text: unspecified
notes.txt: eol: lf
image.bin: text: unset
image.bin: eol: unspecified
other.dat: text: unspecified
other.dat: eol: unspecified
Edited working rule:
report.txt: text: unset
Staged rule:
report.txt: text: set

Read the result

The first rule sets text and gives eol a string value. -text explicitly unsets an attribute, whereas !text resets it to unspecified. Rules can match paths that do not yet exist, which is useful for checking policy before adding files. After editing .gitattributes, the ordinary query sees the edit while --cached reads the index version.

Apply it carefully

This command reports attributes; it does not rewrite a file or prove that its current bytes use the reported line-ending policy. Higher-precedence repository information attributes and other applicable attribute files can affect real repositories. Changing text rules can produce broad diffs when files are later renormalized, so review that as a separate operation. Do not confuse unspecified with explicitly unset when interpreting the output.

The official reference describes the API and its options. The checks above cover the specific fixture, not every possible input or environment.

Clean up

Remove the source and then the empty practice directory:

rm -- demonstrate-check-attr.sh
cd -- "$start_dir" || exit 1
rmdir -- "$work_dir"
unset start_dir work_dir

If the directory is not empty, inspect the remaining files rather than deleting unknown contents.

Acknowledgements

This post was created with assistance from AI.

Last updated on