git 错误:以下未跟踪的工作树文件将被 checkout 覆盖

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25392771/
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-09 02:20:06  来源:igfitidea点击:

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

gitgit-pullgit-status

提问by randomor

When I do git statusit says nothing to commit, working directory clean

当我这样做git status时说nothing to commit, working directory clean

And then I do git pull --rebase, it says:

然后我做了git pull --rebase,它说:

First, rewinding head to replay your work on top of it...
error: The following untracked working tree files would be overwritten by checkout:
    includes/resources/moduledata/12/_Fr4_02_Invention_IPA_SR_la-Fe?te.pdf
Please move or remove them before you can switch branches.
Aborting
could not detach HEAD

Similar error when doing git pull origin master

做类似的错误 git pull origin master

 * branch            master     -> FETCH_HEAD
error: The following untracked working tree files would be overwritten by merge:
    includes/resources/moduledata/12/_Fr4_02_Invention_IPA_SR_la-Fe?te.pdf
Please move or remove them before you can merge.
Aborting

My .gitignorefile:

我的.gitignore文件:

→ cat .gitignore 
.htaccess
bower_components/

This file has been coming up constantly and when I remove it from file system, git will say I removed this file, while in the other messages, it says it is untracked. How could it be untracked and tracked at the same time?

这个文件一直在出现,当我从文件系统中删除它时,git 会说我删除了这个文件,而在其他消息中,它说它没有被跟踪。怎么可能同时不被跟踪和被跟踪?

采纳答案by jub0bs

Without a complete picture of the repo, what follows is more of a guess than anything else, but it might explain the situation. Let's say your history looks as follows:

如果没有完整的回购图片,接下来的内容更多是猜测,但它可能解释了这种情况。假设您的历史记录如下所示:

A -- C [origin/master]
  \ 
   B [HEAD, master]

You write:

你写:

This file has been coming up constantly and when I remove it from file system, git will say I removed this file, while in the other messages, it says it is untracked.

这个文件一直在出现,当我从文件系统中删除它时,git 会说我删除了这个文件,而在其他消息中,它说它没有被跟踪。

I'm guessing you may have run

我猜你可能跑了

git rm --cached <file-in-question>

and committed that deletion in commit B; therefore, the file is no longer tracked in your local repo and but it's still present in your working tree.

并在 commit 中提交删除B;因此,该文件不再在您的本地存储库中进行跟踪,但它仍然存在于您的工作树中。

In the meantime, the upstream branch received commit Cfrom one of your collaborators, in which <file-in-question>was notremoved from version control. What you're attempting to effect with

在此期间,收到了上游分支提交C您的合作者之一,它<file-in-question>不是从版本控制中删除。你试图用什么来影响

git pull --rebase

is something like this:

是这样的:

 A -- C [origin/master]
       \ 
        B' [HEAD, master]

However, as the message says,

然而,正如消息所说,

The [...] untracked working tree [file] would be overwritten by checkout

[...] 未跟踪的工作树 [file] 将被 checkout 覆盖

Indeed, rewinding commit C(in order to replay Bon top of it) would result in the revision of <file-in-question>(from commit C) to be checked out in your working tree, in which an untracked file of the same name already exists. The contents of that untracked file may be valuable; you may not want that file to be overwritten by a another version of it. Therefore, Git stops in its track and tells you what's wrong.

实际上,倒回提交C(为了在其上重放B)将导致在您的工作树中检出<file-in-question>(from commit C)的修订,其中已经存在一个未跟踪的同名文件。该未跟踪文件的内容可能很有价值;您可能不希望该文件被它的另一个版本覆盖。因此,Git 停下来并告诉您出了什么问题。

Edit: Here is a baby example that reproduces the situation...

编辑:这是一个重现这种情况的婴儿示例......

cd ~/Desktop
mkdir test
cd test
git init
touch README.md
git add README.md
git commit -m "add README"

# simulate a remote branch moving ahead by one commit
# (that doesn't remove the README)
git checkout -b origin_master
printf "This is a README.\n" > README.md
git add README.md
git commit -m "add description in README"

# remove the README and create a new commit on master
git checkout master
git rm --cached README.md
git commit -m "remove README"

# simulate an attempt to rebase master to its "upstream" branch, origin_master
git rebase --onto origin_master master

That last command spews out the following:

最后一条命令显示以下内容:

First, rewinding head to replay your work on top of it...
error: The following untracked working tree files would be overwritten by checkout:
    README.md
Please move or remove them before you can switch branches.
Aborting
could not detach HEAD

I suggest you run

我建议你跑

git fetch
git log --stat origin/master..master -- <file-in-question>

to check whether something like that happened.

检查是否发生了类似的事情。

回答by MikeHall

This could also happen due to a case change on the filename. I had the same problem and this is what solved it for me.

这也可能由于文件名的大小写更改而发生。我有同样的问题,这就是为我解决的问题。

git config core.ignorecase true

True for Mac or PC.

适用于 Mac 或 PC。

Alternative solutions at: The following untracked working tree files would be overwritten by checkout

替代解决方案: 以下未跟踪的工作树文件将被检出覆盖

回答by Abhishek Goel

Remove all untracked files (carefull):

删除所有未跟踪的文件(小心):

git clean  -d  -fx ""