Git 在 Windows 上检查以 LF 行结尾的特定文件类型

Gut*_*Haz 6 windows git line-endings

在 Windows 上,我想查看所有以 LF 行结尾的 linux shell 文件 (.sh)

其他基于文本的文件的行结尾应转换为 CRLF。(通过全局 core.autocrlf=true 处理)

  • 全局.gitconfig

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

  • 存储库根文件夹中的 .gitattributes

*.sh text eol=lf

git add --renormalize .在添加 .gitattributes 文件时使用。

不幸的是,.sh 文件在签出后仍然有 CRLF。


附加信息:我的一位团队成员确实在一些提交之前更改了他的全局 core.autocrlf=false ,我猜这导致了混乱的行结尾。

通过上述步骤,我至少可以修复本地存储库的文件以再次以 CRLF 结尾。


尝试的步骤:

  • 本地删除文件并再次签出:无影响 - 全部 CRLF
  • 删除文件、推送删除、用 LF 重新创建文件:签出后仍为 CRLF
  • 使用 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)

Gut*_*Haz 8

.gitattributes没关系,我的文件名有一个拼写错误

尽管如此,解决方案:

  • 修复 .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
    
    Run Code Online (Sandbox Code Playgroud)
  • 调用git add --renormalize .以修复存储库中带有 CRLF 的文件的行结尾