Git diff 元数据设置并忽略这些

问题描述 投票:0回答:1

如果我删除 git repo 中的文件,然后使用相同的内容重新创建它,我的 Ubuntu 会说

On branch main nothing to commit, working tree clean
但在 Windows 或 Mac 中,git diff 将显示编辑的文件中的每一行,即使文件内没有任何更改。 这是为什么?我们如何改变这种行为?有这方面的 git 设置吗? 我认为 git 应该只跟踪文件内容更改,记录一些磁盘级元数据以查看文件是否被重新创建,但其内容未编辑。 (加上也许执行位等..)

删除 git repo 中的文件,恢复文件并查看 git diff 报告“无更改”,因为文件中没有任何更改,它是完全相同的,程序代码仍然工作相同。

git filesystems repository metadata diff
1个回答
0
投票

Windows 和 UNIX 系统(即 Linux/MacOS)使用不同的行结束序列。

Windows 使用 CR+LF (/r/n),而 UNIX 系统使用 LF ( )仅。

这意味着,如果您在 Windows 中加载文件,它可能会以 CR+LF 行结尾重新保存该文件,这会导致

git diff
表示已进行更改。

可以使用

.gitattributes
文件进行重新规范化 - https://git-scm.com/docs/gitattributes#_end_of_line_conversion

$ echo "* text=auto" >.gitattributes
$ git add --renormalize .
$ git status        # Show files that will be normalized
$ git commit -m "Introduce end-of-line normalization"
© www.soinside.com 2019 - 2024. All rights reserved.