无法切换git分支:"未跟踪文件"但工作目录干净。

12

我想切换分支,但是出现了错误。

error: Untracked working tree file 'foo.phtml' would be overwritten by merge.

如果我删除这个文件,下一个文件就会出现问题。问题在于这两个分支中的文件有很大的差异。

这是我尝试过的:

$ git reset --hard HEAD
HEAD is now at 8a2a95c Initial checkin
$ git checkout otherbranch
error: Untracked working tree file 'foo.phtml' would be overwritten by merge.
$ git status
# On branch master
# Your branch and 'origin/master' have diverged,
# and have 1 and 677 different commit(s) each, respectively.
#
nothing to commit (working directory clean)

这里有什么问题?

我也已经尝试过了。

git clean -f -d
2个回答

13

你面临的问题是,你想要检出的分支包含许多像foo.phtml这样的文件,这些文件在当前分支中没有被跟踪。看起来你可以删除这些文件,所以尝试执行以下命令:

git checkout -f otherbranch

我的同事在同一时间发现了这个问题 :-) 有趣的是,为什么git status显示一个干净的工作目录。 - Alex
1
如果它没有显示未跟踪的文件,那么你的 .gitignore 文件可能会覆盖它们。 - mavam

6

如果您稍后关心这些文件,请使用

git stash -u

这将清理您的工作目录,但如果您遗失了某些内容,可以从stash中取回。现在您可以继续检出其他分支。

另一种情况是文件在一个分支中被忽略,在另一个分支中不被忽略。在这种情况下,您应该在stash命令后(如果git状态显示有更改)跟随着git checkout -f 另一个分支


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