如何让Git更改工作目录中的行尾?

16

我已经在Windows上配置了git,使用命令 git config --global core.autocrlf false,这样git在检出文件时不会自动将LF行尾转换为CRLF。

当我在Windows上创建新文件并添加时,会得到以下输出。

git add windows-file.txt
warning: CRLF will be replaced by LF in windows-file.txt.
The file will have its original line endings in your working directory.

当将windows-file.txt添加到git索引中时,git会将我的行尾从Windows更改为Unix,这正是我想要的。

但是,我的问题是工作目录版本没有更改。我该如何配置git,以便同时更改工作目录和git索引的行尾?

更新:

在添加和提交后,即使本地工作目录版本具有Windows行尾,并且存储库版本具有Unix行尾,git状态也不显示任何差异。

更新: 在存储库根目录的.gitattributes文件中的内容。

# Set default behaviour, in case users don't have core.autocrlf set.
text eol=lf

# These files are text and should be normalized (convert crlf => lf)
*.java       text
*.xml        text
*.cmd        text
*.sh         text
*.txt        text
*.md         text
*.js         text
*.jsp        text
*.html       text
*.htm        text
*.vm         text
.project     text
.classpath   text
*.properties text
*.txt        text
*.bat        text
*.launch     text

# Denote all files that are truly binary and should not be modified.
*.png    binary
*.jpg    binary
*.jar    binary
*.class  binary
*.gz     binary
*.tar    binary
*.dll    binary
*.exe    binary
*.zip    binary

git status 显示文件是否不同(即脏文件)? - asm
1
git config --global 更改全局设置。可能仍然存在本地的每个存储库设置。git config core.autocrlf 显示什么? - mvp
@mvp 我熟悉本地git每个仓库的设置,但会将所有东西设置为全局,因为我还使用egit,它尚未支持.gitattributes。 - ams
你能展示一下你的.gitattributes文件吗? - mvp
请参考此问题以强制文件使用lf:https://dev59.com/questions/B3E85IYBdhLWcg3w_Ixr - stormwild
可能是重复的问题,参考如何在Windows下强制Git使用LF而不是CR + LF? - Lorenz Meyer
1个回答

19

当你没有未提交的更改时,请尝试执行以下操作:

git rm --cached -r . 
git reset --hard

删除本地文件并从索引获取它们。


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