Sub topic 10
Fixing common problems: CRLF scripts, phantom changes, mixed endings
This chapter assumes you know the basic Git workflow (clone, add, commit, status, diff). It also assumes you have read the earlier chapters on text, eol, core.autocrlf, and git add --renormalize. Here you will use them to diagnose three problems that come up most often.
A working method
Most line-ending problems can be solved in the same three steps:
- Ask Git how it classifies the file.
- Find which rule produced that classification.
- Fix the rule, then renormalize.
Two commands cover the first two steps.
# Step 1: what is in the index (i/) and the working tree (w/)?
git ls-files --eol scripts/deploy.sh
# Step 2: which attributes apply to this path?
git check-attr -a -- scripts/deploy.shIn the --eol output, i/crlf means the index holds CRLF. w/crlf means the working tree holds CRLF. The attr/ column shows the attributes in effect.
Problem 1: CRLF in shell scripts
Symptom. A script fails on Linux or in a container with an error such as bad interpreter: /bin/bash^M: no such file or directory. The ^M is the carriage return (CR) character at the end of the first line. The kernel looks for an interpreter literally named /bin/bash\r.
Cause. The script was checked out with CRLF, usually on Windows with core.autocrlf=true or with * text=auto and no eol rule.
Fix. Pin the working-tree ending for scripts.
* text=auto
*.sh text eol=lfThen renormalize and check the result.
git add --renormalize .
git ls-files --eol scripts/deploy.shA correct result shows i/lf w/lf attr/text eol=lf. Developers who already have the file checked out with CRLF may need to refresh it. Commit or stash their changes first. Then run git rm --cached -r . && git reset --hard, or simply delete and re-check-out the affected files.
Problem 2: Phantom changes
Symptom. git status lists files as modified, but git diff shows nothing useful, or it shows every line changed. Switching branches or running git stash does not clear it.
Common causes:
- The index holds CRLF, but your rules now say the file should be LF. Git sees the normalized content as different from what is stored.
- A
.gitattributeschange landed, but nobody renormalized. - Two developers have different
core.autocrlfvalues, so each one's editor or checkout flips endings.
Fix. Look at the i/ and w/ columns. If the index still has CRLF where it should have LF, renormalize.
git add --renormalize .
git status
git commit -m "Normalize line endings"Keep that commit separate from other work. Reviewers can then skip it, and you can add its hash to a .git-blame-ignore-revs file so git blame can skip it.
Problem 3: Mixed endings in one file
Symptom. A file contains both LF and CRLF lines. git ls-files --eol reports i/mixed or w/mixed.
Cause. Someone pasted CRLF text into an LF file, or an editor preserved the endings of the original lines it did not touch.
Behavior to know. With text=auto, Git decides whether a file is text by inspecting its content. A file that mixes endings and was previously committed with CRLF may be left unconverted, because Git avoids changing a file when conversion would not round-trip. An explicit text attribute forces conversion of every line. For a file you know is text, say so.
*.sh text eol=lf
*.md textThen renormalize. After the commit, i/mixed should become i/lf.
Quick reference
| Symptom | Likely cause | First check | Fix |
|---|---|---|---|
| `bad interpreter ^M` | Script has CRLF in working tree | `git ls-files --eol` shows `w/crlf` | Add `eol=lf`, renormalize, re-checkout |
| Files modified, empty diff | Index endings disagree with rules | `i/` vs `attr/` columns | `git add --renormalize .` |
| Rule has no effect | Later rule or other file overrides it | `git check-attr -a` | Fix order or location |
| `i/mixed` | Mixed endings in the file | `git ls-files --eol` | Use explicit `text`, renormalize |
| Binary file corrupted | Matched by `text=auto` or `text` | `git check-attr text` | Add `binary` |
Rules that do not apply
If git check-attr shows a value you did not expect, check two things. First, later lines in .gitattributes override earlier lines for the same attribute, so put general patterns such as * text=auto first. Second, a .gitattributes file in a subdirectory overrides one in a parent directory for files beneath it.
Exercise
A teammate on Windows reports that ./build.sh fails with /bin/bash^M: bad interpreter. git ls-files --eol build.sh shows i/lf w/crlf attr/text=auto. What is wrong, and what do you change?
Show answer
The index is correct (LF), but the working tree has CRLF because nothing forces LF for scripts. Add *.sh text eol=lf to .gitattributes after * text=auto, commit it, and have the teammate re-checkout the file so it is rewritten with LF.
Exercise
After you add * text=auto to an existing repository, git status shows many modified files that look identical in git diff. Which command applies the new rules to the stored content, and why should you commit its result alone?
Show answer
git add --renormalize . re-stages tracked files with the new rules applied. Committing it separately keeps the whitespace-only change out of feature commits, which makes review and git blame easier.