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

我有个 Git 仓库。它有 A B C D E... 承诺。现在我想签出 D 作为一个名为 DBranch 的新分支。所以我执行: git checkout D -b Dbranch。现在我要删除这个分支。首先我需要切换到主分支,然后使用 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

如何删除德布兰奇?

231982 次浏览

do a :

git branch

if git show you something like :

* (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.

Try git checkout -f master.

-f or --force

Source: 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.

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

git switch -f master

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

This command will proceed, 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-submodules is specified, submodule content is also restored to match the switching target.
This is used to throw away local changes.