Reproduce it#
A checkout with core.autocrlf=true rewrites line endings. The repository stores f.sh with LF, and this clone converts it:
git -c core.autocrlf=true clone repo copy
| Original | Copy | |
|---|---|---|
file f.sh | ASCII text | ASCII text, with CRLF line terminators |
sha256sum f.sh | 911169dd… | 58055bdc… |
git ls-files --eol f.sh | i/lf w/crlf |
The index has LF (i/lf) and the working tree has CRLF (w/crlf). Run with Git 2.43 on Linux. The -c option applied only to the clone command, so a later git status in that copy showed M f.sh. With the setting stored in the clone's configuration, git status printed nothing and the file still had CRLF on disk.
A shell script with a CRLF first line does not start:
/bin/bash: line 15: ./x.sh: cannot execute: required file not found
The exit status is 127. The kernel looks for an interpreter named /bin/sh followed by a carriage return. The message names the script, which plainly exists, so people check the path and the permissions first.
Separate the causes#
| Observation | Cause | Fix |
|---|---|---|
i/lf w/crlf | The receiver's checkout converts | Set the attribute below, then check out the file again |
i/crlf w/crlf | CRLF was committed | Normalize once, in its own commit |
i/lf w/lf, and the script still fails | Not line endings, or the copy that fails is not this file | Run file on the copy that fails |
| The file came by chat, email or a zip | Git was not involved | Compare hashes with the sender |
The third row matters for build contexts. A container image can hold a CRLF copy made on one machine while the repository is fine on another, so check the file inside the image.
Make the repository decide#
A .gitattributes entry takes the choice away from each person's configuration:
*.sh text eol=lf
*.bat text eol=crlf
*.png binary
The gitattributes manual describes text and eol, and gives git add --renormalize . for files already stored with the wrong endings. Commit the renormalization alone. It touches whole files and would hide a real change in the same diff.
What to put in the handoff#
Send the hash of the file you ran, not only its path: sha256sum scripts/seed.sh. A matching hash rules out line endings, encoding and an edited copy in one comparison. A different hash tells the receiver to look for conversion before reading the code.
Worked example#
This example is fictional. A QA engineer on Windows reports that seed.sh is missing inside a container that the developer built from the same commit. The developer's hash starts 911169dd and the one inside the image starts 58055bdc. git ls-files --eol on the QA machine prints i/lf w/crlf. The attribute line fixes the next checkout, and the bug report about a missing file is closed with the two hashes.
Files sent outside Git have more ways to change. Start from handing over evidence.