Git状态显示修改,但使用git checkout -- <file>命令无法删除它们。

260

我想要删除我的工作副本中的所有更改。
运行git status会显示已修改的文件。
无论我做什么都不能删除这些修改。
例如:

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")

6
不确定为什么 git reset --hard 在这里没起作用。注意:应该使用 git checkout -- \git ls-files -m` 命令来取消文件(--`)。 - VonC
2
如果您删除了文件并使用 checkout -- <file>,它应该可以工作。在某些情况下(包括存在 CRLF 不匹配的情况),更改检测可能有点挑剔。 - eckes
可能是无法在Git中放弃更改的重复问题。 - BuZZ-dEE
1
@BuZZ-dEE - 这是一个重复的问题,而且旧的问题会优先考虑。但是,虽然您链接到的问题本质上是相同的,但我下面接受的答案比那里的任何答案都更具信息量,并解决了我的问题。 - rbellamy
“git update-index --assume-unchaged” 是一种针对无聊情况的多功能工具,它可以强制将文件保持为未更改状态(即使它们已经被更改了!)。 - pdem
25个回答

0

我通过编辑.git/config文件来解决这个问题,添加了以下内容:

[branch "name_branch"]
    remote = origin
    merge = refs/heads/name_branch

然后我进入.git/refs/heads/name_branch,将最后一次提交的ID放在enter code here中。


0
这里提出的一个解决方案没有起作用,我发现该文件实际上是一些特殊字符的链接。
% ls -l StoreLogo.png
lrwxrwxrwx 1 janus janus 8 Feb 21 10:37 StoreLogo.png -> ''$'\211''PNG'$'\r\n\032\n'

% git status    
Changes not staged for commit:
    modified:   StoreLogo.png

% git rm --cached -r StoreLogo.png
rm 'src/GWallet.Frontend.XF.UWP/Assets/StoreLogo.png'

% git reset StoreLogo.png         
Unstaged changes after reset:
M   src/GWallet.Frontend.XF.UWP/Assets/StoreLogo.png

% git status                      
Changes not staged for commit:
    modified:   StoreLogo.png

0

我是这样解决的:

  1. 复制你想要的正确代码的内容
  2. 从你的磁盘中删除导致问题的文件(即无法还原的文件)。现在,你应该会发现同一文件的两个版本都被标记为已删除。
  3. 提交文件的删除操作。
  4. 使用相同的名称创建文件,并粘贴你在步骤1中复制的正确代码
  5. 提交新文件的创建操作。

那是对我有效的方法。


0
我们公司也遇到了类似的情况。提出的方法都没有帮助我们解决问题。经过研究,问题终于被发现了。问题在于Git中有两个文件,它们的名称只有符号的大小写不同。Unix系统将它们视为两个不同的文件,但Windows却无法处理。为了解决这个问题,我们在服务器上删除了其中一个文件。之后,在Windows本地仓库中使用了以下几个命令(以不同的顺序)来帮助解决问题:

git reset --hard
git pull origin
git merge

0

.gitattributes与

test_file_with_crlf text eol=crlf 

实际上,该文件与存储库中的CRLF不匹配(可能是由于core.autocrlf=false而进行的检查)。

解决方案:

  1. 使用autorcrlf=input重新添加文件(参见git add --renormalize)
  2. 在此提交之前尽量不要对任何内容进行操作。

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