Sub topic 7
Checking what Git will do to a file
This chapter assumes you can clone, add, commit and read git status output. It also assumes you know what a .gitattributes file is and that LF and CRLF are different line endings. Here you will learn to inspect how Git classifies a file, so you can stop guessing when line endings misbehave.
Two questions to ask
Git applies two kinds of information to every tracked file. Inspection means checking both.
- What line endings does the file contain right now, in the repository and in your working tree?
- Which attributes apply to this path, and what do they tell Git to do?
Two commands answer these. git ls-files --eol answers the first, and git check-attr answers the second.
Reading git ls-files --eol
The index is Git's staging area. It holds the version of each file that the next commit will record. The working tree is the set of files on disk. The --eol option reports the line endings of both.
git ls-files --eolSample output:
i/lf w/crlf attr/text=auto eol=lf scripts/deploy.sh
i/lf w/lf attr/text=auto README.md
i/none w/none attr/-text logo.png
i/mixed w/mixed attr/ legacy/notes.txtEach line has four parts: the index state (i/), the working-tree state (w/), the attributes in effect (attr/), and the path.
| Value | Meaning |
|---|---|
| `lf` | Every line ends with LF |
| `crlf` | Every line ends with CRLF |
| `mixed` | The file contains both LF and CRLF endings |
| `none` | The file has no line endings, or Git treats it as binary |
| `-text` | In `i/` and `w/` this means Git detected binary content |
The first line above shows deploy.sh stored with LF in the index but checked out with CRLF, even though its rule says eol=lf. That mismatch means the file was checked out before the rule existed. The next chapter covers fixing it. For now, the useful skill is spotting it.
A mixed index state is the most important warning sign. It means the committed file itself has inconsistent endings, and no setting will make diffs clean until you fix that file.
You can limit the output to a few paths:
git ls-files --eol -- scripts/ docs/To find problem files quickly, filter the output:
git ls-files --eol | grep -E "i/mixed|w/crlf"Reading git check-attr
git ls-files --eol shows a summary of the attributes. git check-attr shows exactly how the rules in your .gitattributes files resolve for a path.
git check-attr -a -- scripts/deploy.shSample output:
scripts/deploy.sh: text: set
scripts/deploy.sh: eol: lfThe -a option lists every attribute that applies to the path. To check particular attributes across several paths, name them before the --:
git check-attr text eol -- scripts/deploy.sh README.md logo.pngSample output:
scripts/deploy.sh: text: set
scripts/deploy.sh: eol: lf
README.md: text: auto
README.md: eol: unspecified
logo.png: text: unset
logo.png: eol: unspecifiedThe values mean the following:
setmeans the attribute is on, as intext.unsetmeans the attribute is turned off, as in-text. Thebinarymacro produces this.unspecifiedmeans no rule mentions the attribute.- Any other word is a value, such as
lforauto.
git check-attr works on paths that do not exist yet, so you can test a rule before creating a file.
A diagnostic routine
When a file shows unexpected changes or breaks on another platform, follow this order.
- Run
git ls-files --eol -- path/to/fileto see the index and working-tree endings. - Run
git check-attr -a -- path/to/fileto see which rules apply. - Compare the two. A rule that says
eol=lfwithw/crlfin the output means the working tree is stale. Amixedindex state means the stored file needs cleaning.
Common mistakes
- Reading
attr/as the file's current state. It shows only which rules apply, not whether the file already follows them. - Running
git check-attrwith the path before the--. Put attribute names first, then--, then paths. - Checking an untracked file with
git ls-files --eol. It lists tracked files only. Usegit addfirst, or usegit check-attrfor the rule lookup.
Exercise
git ls-files --eol prints "i/lf w/crlf attr/text=auto eol=lf scripts/run.sh". What does this tell you, and which command would you run next to see how the rules resolve for this file?
Show answer
The committed copy uses LF, but the copy on disk uses CRLF, although the attributes say it should have LF. The working tree is out of step with the rule, probably because the file was checked out before the rule was added. Run "git check-attr -a -- scripts/run.sh" to confirm which attributes apply.
Exercise
What do the values "set", "unset" and "unspecified" mean in git check-attr output? Give one example of a line that produces "unset".
Show answer
"set" means the attribute is on, "unset" means it is explicitly turned off, and "unspecified" means no rule mentions it. A path matched by "*.png binary" shows "text: unset", because the binary macro includes -text.