git
Pro Git
Atlassian Git Tutorial
Pro Git 简体中文翻译
GitX
xlinux
V2EX  ›  git

git 文件 EOL 的疑惑

  •  
  •   xlinux · Feb 5, 2024 · 1846 views
    This topic created in 849 days ago, the information mentioned may be changed or developed.

    windows 没有设置 autocrlf,linux 也没有设置 autocrlf

    windows 添加 a 文件,eol 格式为 crlf, 提交 git push

    linux 添加 b 文件,eol 格式为 lf,提交 git push

    windows 拉取文件 git pull

    linux 拉取文件 git pull

    在 windows 查看文件 a 、b 的 eol 格式都是 crlf

    在 linux 查看文件 a 、b 的 eol 格式都是 lf

    github.com 网页下载 zip ,Download ZIP ,查看文件 a 为 crlf ,b 为 lf ,为什么?

    3 replies    2024-02-05 11:07:19 +08:00
    CLMan
        2
    CLMan  
       Feb 5, 2024
    ·EOL·是 end of line 的缩写,表示仓库中文本文件应当使用的换行符,可以通过`core.eol`( etc/gitconfig,~/.gitconfig )来设定,或者.gitattributes 的`* text=auto eol=lf`类似指令来设定,默认为 native 。

    `core.autocrlf`( etc/gitconfig,~/.gitconfig )可以指定当文件的 eof 与设定不同时,是否在检出和提交代码时进行转换。

    回答里的问题:

    1. “在 windows 查看文件 a 、b 的 eol 格式都是 crlf”,因为你没在~/.gitconfig 中设定,所以使用 etc/gitconfig 里面的默认设定,如果你是手动安装的 git ,在你安装时的选项会最终保存在该文件,不同 client ,比如 github desktop 或者 fork 等,可能会使用它们自带的 git
    2. “在 linux 查看文件 a 、b 的 eol 格式都是 lf”与 1 同理
    3. “ZIP ,查看文件 a 为 crlf ,b 为 lf ”,ZIP 里面存储的是原始文件,并不会受到 autocrlf,eof 的设置影响

    特别是 3 ,这些设置本质上是影响 git 这个软件的逻辑,并不会直接影响实际存储的数据
    CLMan
        3
    CLMan  
       Feb 5, 2024
    @CLMan 补充一下,我实际测试过:

    - eol 设定不存在强制效果,只会在提交时当本地文件的 eol 与设定和存档文件均不一致时才提示转换
    - 如果本地文件的 eof 与存档文件的 eof 一致,即使违背了 eol 设定,那么 git 不会提示也不会转换
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   6011 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 02:05 · PVG 10:05 · LAX 19:05 · JFK 22:05
    ♥ Do have faith in what you're doing.