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

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

git checkout master error: the following untracked working tree files would be overwritten by checkout

gitgit-checkout

提问by Fei Xue

I have a git repository. It has A B C D E ... commits. Now I want to checkout D as a new branch named Dbranch. So I excute:git checkout D -b Dbranch. And now I want to delete this branch. Firstly I need to switch to master branch , and then use git branch -d Dbranchto delete it. But when I excute git checkout master, it gives me the error.

我有一个 git 存储库。它有 ABCDE ... 提交。现在我想将 D 签出为一个名为 Dbranch 的新分支。所以我执行:git checkout D -b Dbranch。现在我想删除这个分支。首先我需要切换到 master 分支,然后使用git branch -d Dbranch它来删除它。但是当我执行时git checkout master,它给了我错误。

error: The following untracked working tree files would be overwritten by checkout:
    source/a/foo.c
        ...... (too many lines)
Please move or remove them before you can switch branches.
Aborting

How to delete the Dbranch?

如何删除Dbranch?

回答by dekdev

Try git checkout -f master.

试试git checkout -f master

-for --force

-f或者 --force

Source: https://www.kernel.org/pub/software/scm/git/docs/git-checkout.html

来源:https: //www.kernel.org/pub/software/scm/git/docs/git-checkout.html

When switching branches, proceed even if the index or the working tree differs from HEAD. This is used to throw away local changes.

When checking out paths from the index, do not fail upon unmerged entries; instead, unmerged entries are ignored.

切换分支时,即使索引或工作树与 HEAD 不同,也要继续。这用于丢弃本地更改。

从索引中检出路径时,不要因未合并的条目而失败;相反,未合并的条目将被忽略。

回答by VonC

With Git 2.23 (August 2019), that would be, using git switch -f:

使用 Git 2.23(2019 年 8 月),即使用git switch -f

git switch -f master

That avoids the confusion with git checkout(which deals with files or branches).

这避免了git checkout(处理文件或分支)的混淆

And that will proceeds, even if the index or the working tree differs from HEAD.
Both the index and working tree are restored to match the switching target.
If --recurse-submodulesis specified, submodule content is also restored to match the switching target.
This is used to throw away local changes.

即使索引或工作树与 HEAD 不同,这也会继续。
恢复索引和工作树以匹配切换目标。
如果--recurse-submodules指定,子模块内容也被恢复以匹配切换目标。
这用于丢弃本地更改。

回答by Mali

do a :

做一个:

git branch

if git show you something like :

如果 git 向你展示类似的东西:

* (no branch)
master
Dbranch

You have a "detached HEAD". If you have modify some files on this branch you, commit them, then return to master with

你有一个“分离的头”。如果你修改了这个分支上的一些文件,提交它们,然后用

git checkout master 

Now you should be able to delete the Dbranch.

现在您应该能够删除 Dbranch。