资讯动态

git版本管理工具中的回车换行转换逻辑

发布时间:2026/8/22 10:25:25 来源:尧图企业网站定制
0 Preface/Foreword0.1 参考文档gitattributes配置文件的作用_.gitattributes-CSDN博客1 转换逻辑在Git版本控制系统中core.autocrlf、core.eol和.gitattributes是处理跨平台换行符Line Endings即文件属性的核心机制。Windows使用CRLF\r\n作为换行符二Linux/macOS使用LF\n若配置不当会导致代码审查困难、合并冲突或脚本执行错误。1.1 涉及的相关git变量和git配置文件core.autocrlfcore.eol.gitattributes1.2 core.autocrlf 优先级第二core.autocrlf控制提交和检出双向自动转换逻辑。设置的值对应的行为true:提交转LF检出转CRLF适合Windwos本地开发input:提交转LF检出不转适合macOS/Linuxfalse: 完全不转换原样存储和检出。1.3 core.eol 优先级第三core.eol定义工作区检出换行类型。当且仅当core.autocrlffalse时core.eol设置的值才能生效。core.eol可设置的值为lf: 工作区强制为LF。crlf: 工作区强制为CRLF。native: 工作区工作路径使用操作系统默认换行符。1.4 .gitattributes文件优先级第一.gitattributes是基于路径的文件属性定义是一个版本控制文件需要提交到仓库中允许开发者对特定文件类型或路径定义属性能够解决团队协作中换行符不一致问题的最佳实践Best practice可以覆盖个人的本地配置core.autocrlf和core.eol确保所有协作者的行为一致。该文件的主要功能如下统一换行符处理明确指定哪些文件是文本哪些是二级制以及文本文件应使用哪种换行符。自定义差异比较高数git如何对非文本文件例如pdf等进行diff比较。识别二进制文件防止Git对二进制文件图片、编译生成文件、特定配置文件进行错误的文本合并或换行转换。常用的属性设置如表格常用的属性设置属性设置含义与作用*textauto推荐默认设置。让Git自动检测文件是否为文本。如果是文本提交时规范化为LF检出时根据系统或配置转换。*.cpp text强制将.cpp文件当做文本文件并进行换行符规范化。*.sh text eollf.sh文件视为文本文件并始终在检出时转换为LF。防止在Windwos系统上因检出时含有\r导致脚本无法运行。*.bat text eolcrlf.bash文件视为文本文件并始终在检出时转换为CRLF。Windows系统中批处理文件。*.pngbinary二进制文件申明避免文件合并混乱。在配置/设置.gitattributes文件时要主要不同文件类型的设置可以参考下面的模版# Cross-platform line-ending / binary safety. # # Windows git defaults to core.autocrlftrue. Without the rules below, gits # text-vs-binary heuristic is the only thing protecting binary assets from EOL # conversion — which silently corrupts fonts/images on Windows checkouts. Pin it # explicitly so a clone is byte-identical on every platform. # Default: auto-detect binary, and force LF for everything detected as text. # # The eollf is load-bearing: bare textauto means convert to NATIVE on # checkout, so on Windows every text file with no explicit rule below (SConscript, # SConstruct, Kconfig*, proj.conf, board.conf, Doxyfile, .ini, ...) lands as CRLF. # Several of our sdk_patches diffs target SConscript files, and an LF patch # will not apply to a CRLF working file. Only *.bat / *.ps1 below opt back in. * textauto eollf # Source / config — force LF so shell scripts stay runnable under WSL git-bash # and C/Python sources dont churn between platforms. *.sh text eollf *.py text eollf *.c text eollf *.h text eollf *.cpp text eollf *.lds text eollf *.sct text eollf *.json text eollf *.md text eollf *.patch text eollf # Windows-native scripts — keep CRLF. *.bat text eolcrlf *.ps1 text eolcrlf # Binary assets — NEVER apply text/EOL conversion (this is the fonts fix). *.ttf binary *.otf binary *.ttc binary *.ezip binary *.agif binary *.gif binary *.png binary *.jpg binary *.jpeg binary *.bmp binary *.bin binary *.img binary *.hex binary *.elf binary *.lib binary *.a binary *.o binary *.so binary *.dll binary *.exe binary *.cer binary *.der binary *.pdf binary *.zip binary1.5 仓库行尾符的标准化不管是GitLab还是GitHub仓库索引中文本文件的的行尾符为LF。

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价