简体   繁体   English

使用 ls 命令时,删除的文件夹仍然出现在 git bash 中

[英]The deleted folder still appears in git bash when using ls command

For a C# project, I used git rm -r --cached obj/ and git rm -r -f obj/ command to remove obj/ from the directory, but after that when I used ls I could still see obj/ folder in the directory.对于 C# 项目,我使用git rm -r --cached obj/git rm -r -f obj/命令从目录中删除obj/ ,但之后当我使用ls我仍然可以在目录中看到obj/文件夹目录。 When I tried to use git rm -r obj/ command again, it told me fatal: pathspec 'obj/' did not match any files , but I could still use cd obj/ to see all the files that were supposed to be deleted.当我再次尝试使用git rm -r obj/命令时,它告诉我fatal: pathspec 'obj/' did not match any files ,但我仍然可以使用cd obj/查看所有应该被删除的文件。

What does this mean?这是什么意思?

git rm (without --cached ) should remove those files from the working tree (your disk) as well as the index, provided they were not ignored in the first place. git rm (没有--cached )应该从工作树(您的磁盘)以及索引中删除这些文件,前提是它们首先没有被忽略。

Check if:检查是否:

  • there was an ignore rule already in place已经有一个忽略规则

    git check-ignore -v obj/a_file_inside_obj
  • if all files are still there, or only a few (which might not be deleted, because already used by an active thread)如果所有文件仍然存在,或者只有少数文件(可能不会被删除,因为已被活动线程使用)

Every file you use in Git has up to three simultaneously active copies.您在 Git 中使用的每个文件都有最多三个同时处于活动状态的副本。 For concreteness, let's talk about the file named obj/a (I'll just assume there was such a file).为了具体起见,让我们谈谈名为obj/a的文件(我假设有这样一个文件)。

There is one copy of obj/a in three places when you start:开始的时候三个地方都有一个obj/a副本:

  • One is—or might be, anyway—in the commit that you selected with git checkout master or git switch master or however it is that you selected a branch and therefore a commit.一个是——或者可能是——在你用git checkout mastergit switch master选择的提交中,或者你选择了一个分支,因此选择了一个提交。

  • A second copy of obj/a in Git's index . Git索引obj/a第二个副本。 The index, which is also called the staging area , holds a copy 1 of each file ready to go into the next commit you will make.索引,也称为暂存区,保存每个文件的副本1 ,准备进入您将进行的下一次提交。 The copy in the index is first put there by your git checkout or git switch command.索引中的副本首先由您的git checkoutgit switch命令放在那里。 It stays there until you do something to or about it.它会一直停留在那里,直到您对它做某事或为此做某事。

  • Finally, the only copy of obj/a that you can see is an ordinary file in your work-tree or working tree .最后,您可以看到obj/a的唯一副本是您的工作树工作树中的普通文件。

The reason for the committed copy is that every commit holds a copy of all of your files, as the commit's snapshot.提交副本的原因是每次提交都保存所有文件的副本,作为提交的快照。 This copy, once made, can never be changed at all.此副本一旦制作完成,就永远无法更改。 No part of any commit can ever be altered: all commits are frozen for all time.任何提交的任何部分都不能被更改:所有提交都被永久冻结。

The reason for the work-tree copy is that the committed copy is not only frozen for all time, it's also compressed into a special Git-only format, that only Git can use.使用工作树副本的原因是提交的副本不仅一直处于冻结状态,而且还被压缩为一种只有 Git 才能使用的特殊 Git-only 格式。 This is good for archival but useless for actually getting any work done.这有利于存档,但对于实际完成任何工作无用。 Git has to extract it and turn it back into a normal everyday file, so that you can use it. Git 必须将其解压并转换回普通的日常文件,以便您可以使用它。 This is also why your work-tree is called "work-tree" or "working tree": it's where you do your work.这也是您的工作树被称为“工作树”或“工作树”的原因:它是您工作的地方。

The index copy is the one that doesn't really have an obvious reason to exist.索引副本是没有真正存在明显理由的副本。 But Git has one.但是 Git 有一个。 2 2

When you ran:当你跑:

git rm -r --cached obj/

Git removed the index copy of obj/a . Git 删除了obj/a索引副本。 The committed copy can't be removed: it's in a commit, and no part of any commit can ever be changed.提交的副本无法删除:它在提交中,并且任何提交的任何部分都不能更改。 The work-tree copy of obj/a , which is the one you can see and use, was left behind, because you told Git to leave it alone when you said --cached . obj/a工作树副本(您可以看到和使用的副本)被留下了,因为当您说--cached时,您告诉 Git --cached So now you're down to two copies of the file, instead of three.所以现在你只剩下文件的两个副本,而不是三个。

If you want to get rid of the work-tree copy of the file, use your computer's "remove a work-tree file" command, whatever that is ( rm , perhaps).如果您想删除文件的工作树副本,请使用计算机的“删除工作树文件”命令,无论是什么( rm ,也许)。 Had you told Git earlier git rm -r obj/ you would have instructed Git to remove two copies: the one in the index, and the one in the work-tree.如果你之前告诉 Git git rm -r obj/你会指示 Git 删除两份副本:一份在索引中,一份在工作树中。 Now that the index copy is gone , git rm won't remove the work-tree one, because the work-tree copy is now an untracked file .现在索引副本消失了git rm不会删除工作树,因为工作树副本现在是一个未跟踪的文件 So now you need to use the non-Git command.所以现在你需要使用非 Git 命令。

(Alternatively, you could git add obj/a , which will copy the file into the index. Now it will be in all three places again, and now git rm obj/a will remove both the index and work-tree copies.) (或者,您可以git add obj/a ,它将文件复制索引中。现在它将再次出现在所有三个位置,现在git rm obj/a将删除索引和工作树副本。)


1 Technically, what's in the index is not an actual copy of the file, but rather the file's name and mode and a reference to the file's data, ready to be put into a new commit. 1从技术上讲,索引中的内容不是文件的实际副本,而是文件的名称和模式以及对文件数据的引用,准备好放入新的提交中。 But unless you start inspecting the index's content directly with git ls-files --stage , or using git update-index to change the index's content, the difference here is not all that important.但是,除非您直接使用git ls-files --stage开始检查索引的内容,或者使用git update-index更改索引的内容,否则这里的区别并不是那么重要。

2 That is, Git has a copy of the file there—although see footnote 1. Git has a reason, too: it makes it easier for Git to be Git. 2也就是说,Git 在那里有一份文件副本——尽管参见脚注 1。Git 也有一个原因:它使 Git 更容易成为 Git。 There are systems that work a lot like Git, but don't bother with an index.有些系统的工作方式与 Git 非常相似,但不必为索引而烦恼。 They just use the work-tree copy as the proposed next commit.他们只是使用工作树副本作为建议的下一次提交。 That works, but it's slower and doesn't offer some of the features that Git does.这可行,但速度较慢,并且不提供 Git 提供的某些功能。

git rm just removes files from git. git rm只是从 git 中删除文件。 If you want to remove the files from the file system, you should use rm -rf ./obj .如果要从文件系统中删除文件,则应使用rm -rf ./obj

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM