git checkout不能重置文件

8

我正在运行安装了Git-1.8.3-preview20130601.exe的Windows 7。

我在我的工作目录中有一个修改过的文件,我想把它恢复到上次提交时的状态。我尝试使用各种不同的git checkout命令,但是修改后的文件仍然存在于我的工作目录中。例如:

[C:\Work\BitBucket\proj1] 14:32:45>git status
On branch work2
Your branch is behind 'origin/work2' by 9 commits, and can be fast-forwarded.
  (use "git pull" to update your local branch)
Changes not staged for commit:
   (use "git add <file>..." to update what will be committed)
   (use "git checkout -- <file>..." to discard changes in working directory)

       modified:   pomodoro.html
 no changes added to commit (use "git add" and/or "git commit -a")

[C:\Work\BitBucket\proj1] 14:32:53>git checkout pomodoro.html

[C:\Work\BitBucket\proj1] 14:33:00>git status
 On branch work2
 Your branch is behind 'origin/work2' by 9 commits, and can be fast-forwarded.
   (use "git pull" to update your local branch)

 Changes not staged for commit:
   (use "git add <file>..." to update what will be committed)
   (use "git checkout -- <file>..." to discard changes in working directory)

       modified:   pomodoro.html
 no changes added to commit (use "git add" and/or "git commit -a")

[C:\Work\BitBucket\proj1] 14:33:04>

我使用git checkout -- pomodoro.html 命令得到了相同的结果。

因此,git 认为 pomodoro.html 文件已被修改,但当我使用该命令时却没有任何变化。

git difftool  

我被告知这些文件是一样的。

不确定是否相关,但是在我的Git配置中,core.autocrlf被设置为true。 当我将其更改为false时,我得到了相同的行为。


@MarkusUnterwaditzer 他试过了。 - millimoose
@millimoose:我还没有提交更改,我只是想撤销未暂存的更改。“git checkout HEAD pomodoro.html”也不起作用… - Number8
也许索引与存储库不同步了? - Number8
如果有差异,可以尝试提交 git commit -va 来查看差异。 - AD7six
@Number8 有人应该把那个作为答案,这样我们就知道那是解决方案了。 - user456814
显示剩余4条评论
3个回答

5
此链接(由AD7Six提供)-- help.github.com/articles/dealing-with-line-endings -- 看起来是答案:修复行尾。
由于autocrlf已设置为true,不确定那个文件中的行尾怎么搞乱了。
感谢其他评论。

请注意,如果您已经弄乱了文件,那么在正确设置配置之后将无济于事。在这种情况下,我建议删除自动修改的文件,然后再次检查它们。现在,更新的配置应该会生效,并帮助您避免这个噩梦。 - Shatu

3
我曾遇到类似的问题,我的操作系统是osx,问题出在两个文件同名但大小写不同:https://sourcedevelop.org/blog/post/fix-git-commands-not-working-some-files。我花了好几个小时才找到解决方法,按照这些指示去尝试,最终发现关键是识别出同名但大小写不同的文件。当你在文件上执行git checkout后,如果再执行git status,你会发现该文件已经被列出来了,但是文件名的大小写不一样。我最终删除了其中一个文件,然后重新添加另一个文件。
git rm -f <filename>
git rm -f <filename>
git commit -m "Removing duplicate files"
<put the file back, the version you want>
git add . 
git commit -m "adding files back"

2

这可能是徒劳的猜测,但:

这个文件在仓库中是否被标记为可执行文件呢?虽然对于 HTML 文件来说是不太可能的,但如果它存储在格式为 FAT 的驱动器上,这种情况是可以想象的。如果是这种情况,而你的 Windows Git 没有配置忽略权限,那么这个文件将始终被更改,因为没有重置或检出操作能在 Windows 上给你可执行位。

要消除这个消息,请将文件模式设置为 false:https://superuser.com/a/139044/236874


我使用标记为可执行文件的文件,并且它们在Windows的Git中运行良好。没有因此产生任何问题(唯一的问题是当我想要创建一个可执行文件时,在Windows中我找不到方法,所以我在某些Unix上进行checkout、设置、提交和推送)。 - user1046334
也许这在过去已经得到改进。几年前我遇到了一些问题。如果作者不知道重置或者确实存在某些真正的问题,让我们等待他的回复。 - mnagel
我刚刚遇到了一个PDF文件的问题。当使用git上传时,它看起来歪斜,但通过FTP上传时却正常。最终发现无法通过checkout或stash来解决问题,这就带我来到了这里,一切都得到了解决。非常感谢。 - mgPePe

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