无法解决的 Git 错误:以下未跟踪的工作树文件将被 checkout 覆盖

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8839247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 06:20:38  来源:igfitidea点击:

Unresolvable Git error: The following untracked working tree files would be overwritten by checkout

gitbranch

提问by ygoe

I am using Git alone for my local software project in Visual Studio 2010. Recently I created a new branch to do a larger refactoring of one of the dialogue windows. I did the following modifications:

我在 Visual Studio 2010 中将 Git 单独用于我的本地软件项目。最近我创建了一个新分支来对其中一个对话窗口进行更大的重构。我做了以下修改:

  • Rename Form1 to Form1a (including all depending files)
  • Add new Form1
  • 将 Form1 重命名为 Form1a(包括所有依赖文件)
  • 添加新的 Form1

I checked this change into the branch, say form-refactoring. Interestingly, Git didn't notice that I renamed the file Form1.cs into Form1a.cs and created a brand new, totally different Form1.cs, but instead it noticed a new Form1a.cs file and found a whole lot of differences between the previous and new Form1.cs files. This will of course lead to totally garbaged diffs, but I don't care in this case as long as all files are handled correctly in the end.

我将此更改检查到分支中,例如形式重构。有趣的是,Git 没有注意到我将文件 Form1.cs 重命名为 Form1a.cs 并创建了一个全新的、完全不同的 Form1.cs,而是注意到了一个新的 Form1a.cs 文件,并发现它们之间存在很多差异以前的和新的 Form1.cs 文件。这当然会导致完全垃圾的差异,但在这种情况下我不在乎,只要最后所有文件都得到正确处理。

Then I switched back to master to do some other small changes. Nothing conflicting. Until now, everything worked fine.

然后我切换回 master 做一些其他的小改动。没有任何冲突。到目前为止,一切正常。

Today, I wanted to switch back to my branch form-refactoring to continue that work. But all I get is the following message:

今天,我想切换回我的分支形式重构来继续这项工作。但我得到的只是以下消息:

git.exe checkout    form-refactoring

Aborting
error: The following untracked working tree files would be overwritten by checkout:
Form1.Designer.cs
Please move or remove them before you can switch branches.

What is that supposed to be? The mentioned file is not untracked. Neither in the master branch, nor in the form-refactoring branch. It is part of both branches, but one is not a descendent of the other. What would happen if I delete it, is it gone for good then? I don't trust Git to bring back the correct file if I delete something now. I did not play with any file at all outside of my mentioned Git operations, so why should I play around with any file to continue using Git operations now? Git broke it, Git's supposed to handle it now!

那应该是什么?提到的文件不是未跟踪的。既不在 master 分支,也不在 form-refactoring 分支。它是两个分支的一部分,但一个不是另一个的后代。如果我删除它会发生什么,那么它就永远消失了吗?如果我现在删除某些内容,我不相信 Git 会带回正确的文件。除了我提到的 Git 操作之外,我根本没有使用任何文件,那么为什么我现在要使用任何文件来继续使用 Git 操作呢?Git 破坏了它,Git 现在应该处理它!

Right now, I cannot continue with my work because I cannot switch branches. Is there an easy solution to this?

现在,我无法继续我的工作,因为我无法切换分支。有没有简单的解决方案?

Git version is 1.7.6, TortoiseGit is 1.7.3.

Git 版本是 1.7.6,TortoiseGit 是 1.7.3。

回答by ygoe

The core.ignorecase config option was not set and Visual Studio had renamed the .Designer.cs file in case, switching from a capital to lower "D" (or vice versa). That was the problem in the end. It took me some file history breakup (deleting and re-adding files) to resolve this error after setting the option to true. Actually the option was set on one computer, but when cloning the repository, the setting got lost somehow. And then destiny was only waiting for VS to rename that file to catch me.

core.ignorecase 配置选项未设置,Visual Studio 已重命名 .Designer.cs 文件以防万一,从大写切换到小写“D”(反之亦然)。这就是最终的问题。将该选项设置为 true 后,我花了一些文件历史记录中断(删除和重新添加文件)来解决此错误。实际上该选项是在一台计算机上设置的,但是在克隆存储库时,该设置以某种方式丢失了。然后命运只是在等待 VS 重命名该文件以抓住我。

So on Windows, you always need to make sure that these two settings are correct after any init/clone operation:

因此,在 Windows 上,您始终需要确保在任何 init/clone 操作之后这两个设置都是正确的:

git config core.ignorecase true
git config core.autocrlf false

Some tools (TGit, gitscc etc.) don't seem to do it right. It's absolutely necessary for normal operation on Windows, but Git doesn't care if they're not set correctly and just lets you stumble over them not telling you why. It's as much lying as it's helpful in this matter.

某些工具(TGit、gitscc 等)似乎做得不对。这对于 Windows 上的正常操作是绝对必要的,但 Git 不在乎它们是否设置正确,只是让你偶然发现它们而不告诉你原因。在这件事上,它既是在撒谎,又是有帮助的。

回答by Alexandr Priymak

Git doesn't allow to switch branches if there is a possibility of data lose.

如果有可能丢失数据,Git 不允许切换分支。

Generally it is a good idea to commit all your changes before switching to another branch. If you are absolutely sure that you didn't make this change than you can reset your working tree using

通常,在切换到另一个分支之前提交所有更改是个好主意。如果您绝对确定没有进行此更改,则可以使用以下方法重置工作树

git reset --hard HEAD

command. But I strongly don't recommend to do this. Use

命令。但我强烈不建议这样做。用

git stash

command to stash your changes to internal storage. In this case you can always recover your data.

命令将您的更改存储到内部存储中。在这种情况下,您始终可以恢复数据。

回答by Robert Childan

This solved the problem for me:

这为我解决了问题:

rm .git/fs_cache

回答by Cascabel

First of all, Git is not lying to you. The file really is tracked in the branch, and really does exist in the work tree, and really is untracked.

首先,Git 没有骗你。该文件确实在分支中被跟踪,并且确实存在于工作树中,并且确实未被跟踪。

Given that you suggest in comments elsewhere that the file doesn't show up in diffs or even git status, it sounds like you've added it to your gitignore (maybe unintentionally via a wildcard pattern). I know you said you didn't, but if it doesn't show up in the output of git statusas an untracked file, then it's untracked and ignored. To verify this, you can list all files ignored by .gitignore:

鉴于您在其他地方的评论中建议该文件不会出现在 diffs 或 even 中git status,听起来您已将其添加到您的 gitignore (可能无意中通过通配符模式)。我知道你说你没有,但如果它没有git status作为未跟踪文件显示在输出中,那么它就没有被跟踪并被忽略。要验证这一点,您可以列出 .gitignore 忽略的所有文件:

git ls-files -i --exclude-from=.gitignore

You can also list all untracked files, ignored or not:

您还可以列出所有未跟踪的文件,忽略与否:

git clean -xdn

(You seem to say that you're very sure the file is tracked - if it really were, git show HEAD:path/to/filewould show the committed contents of the file. I suspect, however, that that will instead tell "fatal: Path 'path/to/file' does not exist in 'HEAD'".)

(您似乎说您非常确定该文件已被跟踪 - 如果确实是这样,git show HEAD:path/to/file则会显示文件的已提交内容。但是,我怀疑这会告诉“fatal: Path 'path/to/file ' 不存在于 'HEAD'"。)

Another way to verify that the file is untracked and ignored is simply to rename it, then see if git statusreports it as deleted; if it doesn't, it wasn't tracked.

验证文件未被跟踪和忽略的另一种方法是简单地重命名它,然后查看是否将其git status报告为已删除;如果没有,则不会被跟踪。

Assuming this does list the file in question, you should examine the file in your work tree. You can see what version of the file the other branch has using git show other-branch:path/to/file, to compare. If you're satisfied that you don't need the version in your work tree, simply remove it. If you're worried you might need it, rename it or move it out of the directory. If you realize you do need it, you should figure out what ignore pattern is ignoring it, remove that, add it, and commit it. In any case, once you've dealt with the file, you'll be able to switch branches.

假设这确实列出了有问题的文件,您应该检查工作树中的文件。您可以查看另一个分支使用的文件版本git show other-branch:path/to/file,以进行比较。如果您对工作树中不需要该版本感到满意,只需将其删除即可。如果您担心可能需要它,请重命名它或将其移出目录。如果你意识到你确实需要它,你应该找出忽略它的忽略模式,删除它,添加它,并提交它。无论如何,一旦您处理了该文件,您就可以切换分支。

回答by Alexandr Priymak

It seems that XXX branch (form-refactoring) contains this file but YYY doesn't. And now you are trying to move from branch YYY to XXX. Git is afraid that you just forget to add file so it doen't allow to move to another branch.

看起来 XXX 分支(形式重构)包含这个文件,但 YYY 没有。现在您正尝试从分支 YYY 移动到 XXX。Git 担心您只是忘记添加文件,因此它不允许移动到另一个分支。

Use

git status

to determine whether this file (Form1.Designer.cs) is untracked. In this case just commit it and then you can safely move to another branch

以确定此文件 (Form1.Designer.cs) 是否未被跟踪。在这种情况下,只需提交它,然后您就可以安全地移动到另一个分支