Search code examples
windowsgitline-endings

Git check out a specific file type with LF line endings on Windows


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)


Solution

  • 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