Learning center

A script fails on your colleague's machine because its line endings changed

When a script that works for you fails for the receiver with "required file not found" or a different hash, compare line endings first. The command git ls-files --eol shows what Git stores and what is on disk, and those can differ while git status is clean.

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
OriginalCopy
file f.shASCII textASCII text, with CRLF line terminators
sha256sum f.sh911169dd…58055bdc…
git ls-files --eol f.shi/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#

ObservationCauseFix
i/lf w/crlfThe receiver's checkout convertsSet the attribute below, then check out the file again
i/crlf w/crlfCRLF was committedNormalize once, in its own commit
i/lf w/lf, and the script still failsNot line endings, or the copy that fails is not this fileRun file on the copy that fails
The file came by chat, email or a zipGit was not involvedCompare 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.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading