7

On Windows, I would like to check out all linux shell files (.sh) with LF line endings

Line Endings of other text-based files should be converted to CRLF. (which is handled via the global core.autocrlf=true)

  • global .gitconfig

[core] editor = 'C:/Tools/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin autocrlf = true

  • .gitattributes in repository root folder

*.sh text eol=lf

I used git add --renormalize . when adding the .gitattributes file.

Unfortunately the .sh files still have CRLF after checkout.


additional information: One of my team members did change his global core.autocrlf=false some commits ago, which caused the chaotic line endings, I guess.

With above mentioned steps I could at least fix files of the local repository to have CRLF endings again.


steps tried:

  • delete files locally and checkout again: no affect - all CRLF
  • delete files, push deletion, recreate files with LF: still CRLF after checkout
  • manually change line endings with Notepad++...

user@workstation MINGW64 /c/repos/project-source (bug_sh_files_eol_lf) $ git status On branch bug_sh_files_eol_lf Your branch is up to date with 'origin/bug_sh_files_eol_lf'. Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: production_code/build_production_code.sh modified: test_code/unit_tests/create_unit_test_xml.sh no changes added to commit (use "git add" and/or "git commit -a") user@workstation MINGW64 /c/repos/project-source (bug_sh_files_eol_lf) $ git add . -u warning: LF will be replaced by CRLF in production_code/build_production_code.sh. The file will have its original line endings in your working directory warning: LF will be replaced by CRLF in test_code/unit_tests/create_unit_test_xml.sh. The file will have its original line endings in your working directory user@workstation MINGW64 /c/repos/project-source (bug_sh_files_eol_lf) $ git status On branch bug_sh_files_eol_lf Your branch is up to date with 'origin/bug_sh_files_eol_lf'. nothing to commit, working tree clean user@workstation MINGW64 /c/repos/project-source (bug_sh_files_eol_lf)

3
  • Simply try deleting all *.sh files and checking them out again to be on the safe side. Commented May 5, 2020 at 8:48
  • Without access to the actual repository, it is hard to be sure, but I suspect that you have existing committed copies of these *.sh files that have CRLF line endings in the committed version of the file. (Normally, with core.autocrlf = true, this wouldn't happen, but there are various ways to make it happen, after which the files stay that way, in commit after commit, until you change them.) If this is the case then the thing to do is fix the file once, manually, e.g., with dos2unix, add, and commit. Commented May 5, 2020 at 14:36
  • 1
    The add-and-commit will make a new commit in which the file has LF-only line endings, and from this point on, the .gitattributes entry will handle things for all new commits. But note: if this guess is wrong, then after using dos2unix or equivalent and git add, Git will say that there is nothing to commit. Commented May 5, 2020 at 14:38

1 Answer 1

9

nevermind, I had a typo in my .gitattributes file name

nevertheless, the solution:

  • fix .gitattributes

    # normalize all introduced text files to LF line endings (recognized by git)
    *           text=auto
    # additionally declare text file types
    *.sh        text eol=lf
    *.c         text
    *.h         text
    *.txt       text
    *.yml       text
    
  • call git add --renormalize . to fix line endings of files with CRLF in repository

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.