【问题标题】:Git won't revert or commit a file that it thinks is modifiedGit 不会还原或提交它认为已修改的文件
【发布时间】:2013-04-11 03:42:04
【问题描述】:

我已按照this tutorial 将一个 SVN 存储库转换为 Git。现在似乎无法像this answer 中建议的那样提取子存储库。

请原谅这篇长文,但大部分文字都是格式精美的 git 输出。

操作系统:Windows 8 命令行:MinGW Git版本:1.8.1.msysgit.1

除非您有一个干净的暂存区并且没有修改过的文件,否则提取子存储库的过程似乎不起作用。

git status 告诉我我有一个修改过的文件,即使这是一个全新的 SVN 导入。好吧,让我们试着摆脱它。

尝试恢复文件。

user$ git status
# On branch master
# 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: folder with space/folder/toolbar.png
#
no changes added to commit (use "git add" and/or "git commit -a")

user$ git checkout -- "folder with space/folder/toolbar.png"

user$ git status
# On branch master
# 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: folder with space/folder/toolbar.png
#
no changes added to commit (use "git add" and/or "git commit -a")

这不起作用,但我不在乎我是否提交它,所以我接下来会尝试。

user$ git commit -a -m "Testing if committing fixes it"
# On branch master
# 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: folder with space/folder/toolbar.png
#
no changes added to commit (use "git add" and/or "git commit -a")

user$ git status
# On branch master
# 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: folder with space/folder/toolbar.png
#
no changes added to commit (use "git add" and/or "git commit -a")

通过跳过 staging 来提交是行不通的,所以让我们先尝试 stage 吧。

user$ git add "folder with space/folder/toolbar.png"

user$ git status
# On branch master
# 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: folder with space/folder/toolbar.png
#
no changes added to commit (use "git add" and/or "git commit -a")

不起作用,所以我很难过...去问问更聪明的人。

我是 git 新手,但对 Hg 很熟悉,并且一直在阅读 this online tutorial 以帮助自己入门。

我完全有可能搞砸了一个简单的命令。

已经尝试过: 我四处寻找解决我的特定问题的方法,但运气不佳。我偶然发现了this answer,这似乎相关但并不能完全解决我的问题。

编辑:可能有趣的事情 这是让我感到困惑的部分。不久前,我已将此 repo 推送到在线存储库中。重新克隆后,repo 仍然认为文件被修改(即git status 返回相同的结果,我已经设置了git config --global core.autocrlf false 并通过运行git config --global core.autocrlf 进行验证,确实返回false)。

编辑 2:找到了修复,但问题仍然不明白 我通过简单地从系统、暂存区域中删除文件然后提交更改来设法修复存储库。 在此之后为了取回文件,我只需将其复制回来并将其提交到存储库。

这个问题虽然已经解决,但让我更加困惑。

当我在玩弄删除文件时,我注意到如果我将存储库重置为 HEAD,其最后一次提交已删除文件,git status 将表明没有任何改变并且文件未被跟踪但文件将恢复到我的工作树中。考虑到它在 git 中被标记为已删除,这很奇怪...

只有在第二次删除它之后,即使 git 不再记得它,我才设法真正删除它,以便 git resetgit reset --hard 不会恢复文件。

如果有人能解释一下我是如何进入这种状态的,如果这是 git 中的错误或正常行为,我将不胜感激。

我的猜想 我丢失了我使用的命令序列,但发生的事情是这样的: 该文件是Images/toolbar.png,我已经导航到Images 文件夹。

在我从文件系统中删除它之后,git 检测到这样的变化:

# On branch master
# 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)
#
#       deleted: toolbar.png
#       deleted: ../images/toolbar.png
#

注意images 文件夹没有大写!这是在忽略路径大小写的 Windows 中运行的。我怀疑这可能是问题的一部分......

我真的很困惑,但我的问题已经消失了。所以这篇文章只是作为一个好奇心,虽然我无法在某个地方复制它在从 SVN 转换中的行为。

【问题讨论】:

  • 似乎 git 可能无法正确处理您的文件。是否可以尝试从文件夹名称中删除空格,看看是否可以解决?
  • 有可能,简单没有……我试试。
  • 现在,我正在尝试提取一个干净的存储库,但我怀疑这无济于事。完成后我会尝试删除空间,但存储库并不小。另外,为什么 git 会遇到路径中的空格问题?
  • 我不希望它在路径名方面有问题,但是看到你的命令没有接受,我认为它值得一看。我有另一个想法。有问题的文件是二进制文件吗?你试过明确告诉 git 这个文件是二进制文件吗?
  • @nonsensical - 您使用哪个 shell 来运行 git 命令 - msysGit shell 还是 Windows 命令行?根据this 页面,在最后,它说命令需要在 Windows 命令行的双引号中。对于 msysGit shell,我相信您需要转义空格(如 *nix),例如:foo\ bar 并且不需要引号。您还可以从 repo 的根目录运行 git add . 以将 git status 列出的所有内容添加为新文件和修改后的文件。

标签: windows git png mingw msysgit


【解决方案1】:

不久前我也遇到过类似的问题。

这个文件的大小写或者它所在的目录是否在任何时候发生了变化?

我有一个目录,其大写字母已更改为全小写(假设它从/Foo 变为/foo)。它给了我你描述的所有相同的问题。

每当我修改文件时,它都会给我类似的输出:

#       modified: bar.txt
#       modified: ../Foo/bar.txt

我也有同样的问题,提交或重置没有产生任何结果。

我认为问题的原因是 Windows 文件路径不区分大小写,但 Unix 是。由于许多命令行工具(如 Git)是在 Unix-y 系统上开发的,因此它们有时无法很好地处理这种差异,并且在将文件添加为 Foo/bar.txtfoo/bar.txt 时会感到困惑。我认为这让 Git 认为有两个不同的文件,实际上只有一个。

我的最终修复与您的相同,从历史记录中删除整个目录,然后重新添加它(并且永远不会再更改大写)。这也引起了你描述的同样的奇怪,我不得不在它之前把它删除两次。

无论如何,我知道这不是一个明确的答案,但我已经能够重现这个问题,所以我很确定这是导致它的原因(至少对我而言)。

【讨论】:

  • 这正是我所经历的。我不确定它发生在哪里,但我确信 git 通过 SHA1 校验和发现它是同一个文件,但由于目录不同而感到困惑......无论哪种方式,你的都是唯一接近的答案我所拥有的。不过,我很乐意接受它作为答案,尤其是如果您可以重现它。
【解决方案2】:

我遇到了类似的问题,原因是两个文件与我的计算机看到的名称相同。

我不断收到的消息:

# On branch master
# 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:   images/contact_seller.GIF
#

发生这种情况是因为 repo 的初始提交是从格式化区分大小写的 MacBook 完成的,并且错误出现在格式化不区分大小写的 iMac 上。

在区分大小写的机器上,您可以看到两个文件

SwedishChef$ ls images/contact_seller*
images/contact_seller.GIF   images/contact_seller.gif

这在第二台机器上是无效的,所以 git 必须对其进行处理。

我只需要重命名文件并提交这些更改。

【讨论】:

    猜你喜欢
    • 2021-12-26
    • 2011-07-18
    • 2021-05-19
    相关资源
    最近更新 更多