无法检出分支或删除有问题的文件。

10

我的Java项目中有2个分支:master和refactor。我已经完成了对refactor的工作,现在想要checkout master并将refactor合并到master中。在进行refactor时,我还将一些文件添加到了.gitignore中(其中一个是.idea),现在我得到了以下提示:

[michal@michal-pc MCleaner]$ git checkout master
error: The following untracked working tree files would be overwritten by checkout:
.idea/description.html
.idea/misc.xml
.idea/modules.xml
.idea/project-template.xml
.idea/vcs.xml
Please move or remove them before you switch branches.
Aborting

我已经阅读了很多帖子,但是没有任何作用。怎样可以在不访问主分支的情况下删除这些文件?有什么办法可以解决这个问题吗?如果您能,可以提供cmd命令。我还是Git新手。

以下是git status的输出:

On branch refactor
Your branch is up-to-date with 'origin/refactor'.

Untracked files: (use "git add <file>..." to include in what will be committed)
  .idea/
  target/

nothing added to commit but untracked files present (use "git add" to track)

你已经将所有更新提交到重构分支了吗? - Thomas B Preusser
1
尽管检出操作声称要覆盖它们,但它们似乎在您的主分支上。 - Thomas B Preusser
还有一个问题,你真的在本地的.gitignore文件中添加了.idea吗? - Allen
强制删除那些未被跟踪的文件并不能真正解决问题,它们可能会再次出现。 - Allen
我只是想知道它们是否在git的黑名单上(在.gitignore中声明),为什么git还会关心它们,.gitignore只是用来忽略那些未跟踪的文件。 - Allen
显示剩余4条评论
4个回答

11

首次运行

get checkout <branch_name>

结果将会类似于以下内容。
error: The following untracked working tree files would be overwritten by checkout:
        .idea/codeStyles/Project.xml
        .idea/codeStyles/codeStyleConfig.xml
        .idea/workspace.xml
Please move or remove them before you switch branches.
Aborting

如果你可以放弃这些文件中的任何数据(它们将被覆盖),请继续运行。
get checkout <branch_name> --force

问题已解决 - 只有一个小提示,命令应该是:git checkout <branch_name> --force - undefined

7
将以下内容添加到.gitignore文件中:
.idea

并删除此目录

git rm -r .idea

然后提交更改。


@Andreas_D 我弄错了,我只把一些文件从.idea添加到.gitignore,而不是整个文件夹。现在运行得很好! - doublemc

1

.gitignore文件中添加那些您不希望跟踪的文件。

作为最佳实践,您应该只跟踪与您的项目相关的源代码和资源文件。不应该跟踪和提交IDE项目特定的设置文件。 通过这样做,项目变得与IDE无关,任何开发人员都可以检出仓库并无缝地工作,而不受他选择的任何IDE的影响。

示例:在.gitignore文件中添加这些内容

.idea         #intellij files
.settings     #eclipse files

注意:类似地,按照这样的方式添加其他IDE设置目录。
然后提交更改。
如果这些文件当前在远程仓库中,则在提交更改之前删除这些文件。

0

请查看手册

git clean --help

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