.gitattributes在Mac和Windows上不能正常工作

6

在我的项目中,我使用不同操作系统的计算机,一个是Mac,另一个是Windows。当我使用git时,每个更改都显示为整个文档的更改。原因是这两个操作系统中的行尾不同。我阅读了这篇文章https://help.github.com/articles/dealing-with-line-endings/并在根文件夹中创建了一个.gitattributes文件,但问题仍然存在。这是我的.gitattributes文件:

# Set the default behavior, in case people don't have core.autocrlf set.
* text=auto

# Explicitly declare text files you want to always be normalized and converted
# to native line endings on checkout.

*.css text
*.html text
*.js text

# Declare files that will always have CRLF line endings on checkout.
*.sln text eol=crlf

# Denote all files that are truly binary and should not be modified.
*.png binary
*.jpg binary

我不知道为什么它不能工作,因为在这之前我尝试了许多此文件的配置。


你尝试过使用 git config core.autocrlf 选项吗?据我所知,这是首选项。你有什么问题吗? - Melebius
是的,我在两台电脑上都尝试了,但它不起作用。 - MaSza
2个回答

10

应在第一次提交时添加 .gitattributes 文件。如果您在几次提交后添加它,则需要显式地规范所有现有文件。

$ rm .git/index     # Remove the index to force Git to
$ git reset         # re-scan the working directory
$ git status        # Show files that will be normalized
$ git add -u
$ git commit -m "Introduce end-of-line normalization"

请查看https://git-scm.com/docs/gitattributes


3

有趣的是,运行git add --renormalize .后,修改了EOL的文件被列在git status中,我可以提交更改,但实际上它们仍然具有原始的EOL(通过十六进制视图验证)。第3步中的命令需要将EOL真正更改为.gitattributes中给定的设置。谢谢! - kol

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接