【问题标题】:git checkout master error: the following untracked working tree files would be overwritten by checkoutgit checkout master 错误:以下未跟踪的工作树文件将被结帐覆盖
【发布时间】:2013-08-02 02:05:49
【问题描述】:

我有一个 git 存储库。它有 A B C D E ... 提交。现在我想将 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

如何删除Dbranch?

【问题讨论】:

标签: git git-checkout


【解决方案1】:

做一个:

git branch

如果 git 向您显示类似的内容:

* (no branch)
master
Dbranch

你有一个“分离的 HEAD”。 如果你在这个分支上修改了一些文件,提交它们,然后返回到 master

git checkout master 

现在您应该可以删除 Dbranch。

【讨论】:

    【解决方案2】:

    试试git checkout -f master

    -f--force

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

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

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

    【讨论】:

    • 啊,救了我的命!看到切换分支后所有现有文件都被删除,我非常紧张。强制结帐后,我已取回所有物品。
    • 我遇到了一个问题,我收到“未跟踪的工作树文件”错误,但 git status -u 没有显示任何内容...我不知道如何删除这个神秘的未跟踪文件,但是这个命令帮助我暂时避开了这个问题
    【解决方案3】:

    对于 Git 2.23(2019 年 8 月),那就是,使用 git switch -f

    git switch -f master
    

    这避免了confusion with git checkout(处理文件或分支)。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-13
      • 2021-12-02
      • 2012-02-08
      • 2011-06-18
      • 1970-01-01
      • 2016-07-27
      • 2017-06-09
      相关资源
      最近更新 更多