git status 显示修改,git checkout -- <file>不会删除它们 [英] git status shows modifications, git checkout -- <file> doesn't remove them

查看:27
本文介绍了git status 显示修改,git checkout -- <file>不会删除它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除对我的工作副本的所有更改.
运行 git status 显示已修改的文件.
我所做的一切似乎都无法消除这些修改.
例如:

I would like to remove all changes to my working copy.
Running git status shows files modified.
Nothing I do seems to remove these modifications.
E.g.:

rbellamy@PROMETHEUS /d/Development/rhino-etl (master)
$ git status
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   Rhino.Etl.Core/Enumerables/CachingEnumerable.cs
#       modified:   Rhino.Etl.Core/Pipelines/SingleThreadedPipelineExecuter.cs
#       modified:   Rhino.Etl.Tests/Rhino.Etl.Tests.csproj
#       modified:   Rhino.Etl.Tests/SingleThreadedPipelineExecuterTest.cs
#
no changes added to commit (use "git add" and/or "git commit -a")

rbellamy@PROMETHEUS /d/Development/rhino-etl (master)
$ git checkout -- Rhino.Etl.Core/Enumerables/CachingEnumerable.cs

rbellamy@PROMETHEUS /d/Development/rhino-etl (master)
$ git status
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   Rhino.Etl.Core/Enumerables/CachingEnumerable.cs
#       modified:   Rhino.Etl.Core/Pipelines/SingleThreadedPipelineExecuter.cs
#       modified:   Rhino.Etl.Tests/Rhino.Etl.Tests.csproj
#       modified:   Rhino.Etl.Tests/SingleThreadedPipelineExecuterTest.cs
#
no changes added to commit (use "git add" and/or "git commit -a")

rbellamy@PROMETHEUS /d/Development/rhino-etl (master)
$ git checkout `git ls-files -m`

rbellamy@PROMETHEUS /d/Development/rhino-etl (master)
$ git status
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   Rhino.Etl.Core/Enumerables/CachingEnumerable.cs
#       modified:   Rhino.Etl.Core/Pipelines/SingleThreadedPipelineExecuter.cs
#       modified:   Rhino.Etl.Tests/Rhino.Etl.Tests.csproj
#       modified:   Rhino.Etl.Tests/SingleThreadedPipelineExecuterTest.cs
#
no changes added to commit (use "git add" and/or "git commit -a")

rbellamy@PROMETHEUS /d/Development/rhino-etl (master)
$ git reset --hard HEAD
HEAD is now at 6c857e7 boo libraries updated to 2.0.9.2 and rhino.dsl.dll updated.

rbellamy@PROMETHEUS /d/Development/rhino-etl (master)
$ git status
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   Rhino.Etl.Core/Enumerables/CachingEnumerable.cs
#       modified:   Rhino.Etl.Core/Pipelines/SingleThreadedPipelineExecuter.cs
#       modified:   Rhino.Etl.Tests/Rhino.Etl.Tests.csproj
#       modified:   Rhino.Etl.Tests/SingleThreadedPipelineExecuterTest.cs
#
no changes added to commit (use "git add" and/or "git commit -a")

推荐答案

有多个问题可能导致这种行为:

There are multiple problems the can cause this behaviour:

行尾规范化

我也遇到过这样的问题.归结为 git 自动将 crlf 转换为 lf.这通常是由单个文件中的混合行尾引起的.该文件在索引中被规范化,但是当 git 然后再次对其进行非规范化以将其与工作树中的文件进行比较时,结果是不同的.

I've had these kinds of problems too. It comes down to git automatically converting crlf to lf. This is typically caused by mixed line endings in a single file. The file gets normalized in the index, but when git then denormalizes it again to diff it against the file in the working tree, the result is different.

但是如果你想解决这个问题,你应该禁用core.autocrlf,把所有的行尾都改成lf,然后再启用它.或者您可以通过以下方式完全禁用它:

But if you want to fix this, you should disable core.autocrlf, change all line endings to lf, and then enable it again. Or you can disable it altogether by doing:

git config --global core.autocrlf false

代替core.autocrlf,您还可以考虑使用.gitattribute 文件.通过这种方式,您可以确保使用存储库的每个人都使用相同的规范化规则,从而防止混合行尾进入存储库.

Instead of core.autocrlf, you can also consider using .gitattribute files. This way, you can make sure everyone using the repo uses the same normalization rules, preventing mixed line endings getting into the repository.

还可以考虑设置 core.safecrlf 以在您希望 git 在执行不可逆规范化时发出警告时发出警告.

Also consider setting core.safecrlf to warn if you want git to warn you when a non-reversible normalization would be performed.

git 手册页这样说:

CRLF 转换有小概率破坏数据.autocrlf=true 会在提交期间将 CRLF 转换为 LF 和结帐时从 LF 到 CRLF.一份文件包含 LF 和 CRLF 的混合物在无法重新创建提交之前通过 git.对于文本文件,这是正确的做法:纠正线条结尾使得我们只有 LF 行在存储库中结束.但对于意外的二进制文件分类为文本转换可以数据损坏.

CRLF conversion bears a slight chance of corrupting data. autocrlf=true will convert CRLF to LF during commit and LF to CRLF during checkout. A file that contains a mixture of LF and CRLF before the commit cannot be recreated by git. For text files this is the right thing to do: it corrects line endings such that we have only LF line endings in the repository. But for binary files that are accidentally classified as text the conversion can corrupt data.

不区分大小写的文件系统

在不区分大小写的文件系统上,当存储库中具有不同大小写的相同文件名时,git 会尝试检出两者,但最终只有一个出现在文件系统上.当 git 尝试比较第二个时,它会将其与错误的文件进行比较.

On case-insensitive filesystems, when the same filename with different casing is in the repository, git tries to checkout both, but only one ends up on the file system. When git tries to compare the second one, it would compare it to the wrong file.

解决方案要么切换到不区分大小写的文件系统,但这在大多数情况下是不可行的,或者重命名并提交另一个文件系统上的一个文件.

The solution would either be switching to a non-case insensitive filesystem, but this in most cases is not feasible or renaming and committing one of the files on another filesystem.

这篇关于git status 显示修改,git checkout -- &lt;file&gt;不会删除它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆