basforge

Search titles, text and the “If it doesn’t work” cases.

    RU

    Set line endings in .gitattributes, not core.autocrlf

    Git for Windows turns LF into CRLF on checkout by default, and a script built on Windows and copied to a server fails: /bin/bash^M: bad interpreter. The rule * text=auto eol=lf in .gitattributes travels with the repository and works the same on every computer.

    Where CRLF comes from

    The Git for Windows installer writes core.autocrlf=true into the system config by default: the repository stores LF, and files are checked out into the working directory with CRLF. Git warns about it itself:

    warning: in the working copy of 'README.md', LF will be replaced by CRLF the next time Git touches it

    While the files live in git, you don’t notice: they go back into the repository with LF. It breaks when a file is copied around git — scp deploy.sh web1: or in an archive — and CRLF lands on the server.

    core.autocrlf is a computer setting, not a repository one: everyone has their own, and on a new laptop it all starts over. .gitattributes lives in the repository itself.

    What to write

    # .gitattributes
    * text=auto eol=lf
    *.ps1 text eol=crlf
    *.cmd text eol=crlf
    *.png binary
    *.jpg binary
    *.zip binary
    • text=auto — git tells text from binary files itself; eol=lf — text always gets LF, both in the repository and in the working directory, on any OS.
    • PowerShell and cmd scripts get CRLF: that’s what Windows expects.
    • binary — don’t touch, and don’t show a line diff.

    Then normalize the files already there, once, and commit:

    git add --renormalize .
    git commit -m "line endings: LF per .gitattributes"

    While you’re at it, set core.autocrlf = false in your own ~/.gitconfig so the system setting stays out of the way (~/.gitconfig block by block).

    If it doesn’t work

    /bin/bash^M: bad interpreter: No such file or directory

    Cause
    The file has CRLF line endings: the kernel looks for a program called /bin/bash\r.
    Fix
    On the server, sed -i 's/\r$//' deploy.sh; for good, .gitattributes and git add --renormalize ..

    $'\r': command not found on every line of a script

    Cause
    The same: CRLF, and bash reads \r as part of the command.
    Fix
    The same.

    git status shows every file as modified, though nobody touched them

    Cause
    core.autocrlf or .gitattributes changed, and git sees different line endings.
    Fix
    One commit: git add --renormalize . && git commit -m "line endings: LF".

    How to check

    git ls-files --eol deploy.sh
    # i/lf    w/lf    attr/text=auto eol=lf   deploy.sh

    i/ is the repository, w/ the working directory. w/crlf on a script meant for a server means it hasn’t been normalized yet.