【问题标题】:How to get git to change the line ending in the working directory?如何让 git 更改以工作目录结尾的行?
【发布时间】:2012-12-29 23:45:24
【问题描述】:

我在配置了git config --global core.autocrlf false 的 Windows 上安装了 git,因此 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 行结尾并且 repo 版本具有 unix 行结尾,在添加和提交之后 git 状态也不会显示任何差异。

更新 .gitattributes 的内容在 repo 的根目录

# 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 是否显示文件不同(即脏)?
  • git config --global 更改全局设置。本地、每个存储库的设置可能仍然有效。 git config core.autocrlf 显示什么?
  • @mvp 我熟悉每个 repo 的本地 git 设置,但是我也使用支持 .gitattributes 的 egit 来设置全局设置
  • 如果你有.gitattributes,你能出示一下吗?
  • 请参考这个问题强制文件使用lf:stackoverflow.com/questions/2517190/…

标签: git


【解决方案1】:

当您没有未提交的更改时尝试:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-09
    • 1970-01-01
    • 2018-05-08
    • 2016-09-14
    • 1970-01-01
    • 2016-03-14
    • 2019-10-02
    相关资源
    最近更新 更多