git 无法删除真正存在的文件 - 致命:pathspec ...没有匹配任何文件

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

unable to remove file that really exists - fatal: pathspec ... did not match any files

gitintellij-idea

提问by Matthew Cornell

unable to remove file that really exists - fatal: pathspec ... did not match any files

无法删除真正存在的文件 - 致命:pathspec ...没有匹配任何文件

I have a file under git control that simply will not be deleted. The failing command is:

我有一个 git 控制下的文件,它不会被删除。失败的命令是:

$ git rm .idea/workspace.xml
fatal: pathspec '.idea/workspace.xml' did not match any files

Below I list the directory contents, branches, etc. So far I've tried rm from within the directory, and escaping just in case there are funny characters, and I'm really stumped. I searched the web and SO but couldn't find this specifically. Thanks in advance!

下面我列出了目录内容、分支等。到目前为止,我已经在目录中尝试了 rm ,并在出现有趣字符的情况下进行转义,我真的很难过。我搜索了网络和SO,但找不到具体的内容。提前致谢!

$ git branch -a
* dot-output
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/modelspace
$ 

$ git status
# On branch dot-output
# Untracked files:
# ...

$ ls .idea/
ant.xml         encodings.xml       modules.xml     workspace.xml
compiler.xml        inspectionProfiles  scopes
copyright       libraries       testrunner.xml
dictionaries        misc.xml        vcs.xml

$ ls -al
total 56
drwxr-xr-x  16 matt  staff    544 Apr 10 11:33 .
drwxr-xr-x@ 33 matt  staff   1122 Apr 10 09:40 ..
-rw-r--r--@  1 matt  staff  12292 Apr 10 11:19 .DS_Store
drwxr-xr-x  18 matt  staff    612 Apr 10 11:39 .git
-rw-r--r--   1 matt  staff     98 Mar  6 13:40 .gitignore
drwxr-xr-x  16 matt  staff    544 Apr 10 11:34 .idea
-rw-r--r--   1 matt  staff   1113 Feb 25 11:07 README
...

$ head -n 2 .idea/workspace.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">

$ git rm .idea/workspace.xml
fatal: pathspec '.idea/workspace.xml' did not match any files

UPDATE: Answers from Nevik and Boris helped a lot. I realized that I was confused by a number of things going on, some of which were exacerbated by IntelliJ IDEA (a tool I generally love, BTW). First, the 'fatal: pathspec' message from git rm is both unhelpful and misleading (in this vanilla user's opinion). Second, I had the file in .gitignore, but had removed it before asking my question. However, it was also included in IDEA's Ignored Files feature independently of git, and showed as such in the project viewer (instead of untracked, as git status showed). Finally, I had IDEA running while I was experimenting, and it looks like it was recreating the file immediately after my rm. So, my takeaway is that if I'm getting confusing Git behavior, make sure to quit IDEA and work soley in the command line (and gitk) while debugging. And that SO users are awesome. Thanks again!

更新:Nevik 和 Boris 的回答很有帮助。我意识到我对正在发生的许多事情感到困惑,其中一些被 IntelliJ IDEA(我通常喜欢的工具,顺便说一句)加剧了。首先,来自 git rm 的“fatal: pathspec”消息既无益又具有误导性(在这个普通用户看来)。其次,我在 .gitignore 中有该文件,但在问我的问题之前已将其删除。但是,它也独立于 git 包含在 IDEA 的忽略文件功能中,并在项目查看器中显示(而不是未跟踪,如 git status 所示)。最后,我在试验时运行了 IDEA,看起来它在我的 rm 之后立即重新创建文件。所以,我的结论是,如果我对 Git 行为感到困惑,请确保在调试时退出 IDEA 并在命令行(和 gitk)中单独工作。SO 用户很棒。再次感谢!

采纳答案by Boris Brodski

Your file .idea/workspace.xmlis not under git version control. You have either not added it yet (check git status/Untracked files) or ignored it (using .gitignore or .git/info/exclude files)

您的文件.idea/workspace.xml不受 git 版本控制。您尚未添加它(检查 git status/Untracked files)或忽略它(使用 .gitignore 或 .git/info/exclude files)

You can verify it using following git command, that lists all ignored files:

您可以使用以下 git 命令来验证它,该命令列出了所有被忽略的文件:

git ls-files --others -i --exclude-standard

回答by gnganpath

$>git add .
$>git rm file_Name  

It works. You add new file using right click -> create new file, and immediately delete it after that. The file would go to the untracked file list.

有用。您使用右键单击添加新文件-> 创建新文件,然后立即将其删除。该文件将转到未跟踪文件列表。

回答by codingatty

I know this is not the OP's problem, but I ran into the same error with an entirely different basis, so I just wanted to drop it here in case anyone else has the same. This is Windows-specific, and I assume does not affect Linux users.

我知道这不是 OP 的问题,但我在完全不同的基础上遇到了同样的错误,所以我只是想把它放在这里,以防其他人有同样的情况。这是特定于 Windows 的,我认为不会影响 Linux 用户。

I had a LibreOffice doc file, call it final report.odt. I later changed its case to Final Report.odt. In Windows, this doesn't even count as a rename. final report.odt, Final Report.odt, FiNaL RePoRt.oDtare all the same. In Linux, these are all distinct.

我有一个 LibreOffice 文档文件,称之为final report.odt. 我后来将其大小写更改为Final Report.odt. 在 Windows 中,这甚至不算作重命名。final report.odt, Final Report.odt,FiNaL RePoRt.oDt都是一样的。在 Linux 中,这些都是不同的。

When I eventually went to git rm "Final Report.odt"and got the "pathspec did not match any files" error. Only when I use the original casing at the time the file was added -- git rm "final report.odt"-- did it work.

当我最终去git rm "Final Report.odt"并得到“路径规范与任何文件不匹配”错误时。只有当我在添加文件时使用原始外壳时 -- git rm "final report.odt"-- 才起作用。

Lesson learned: to change the case I should have instead done:

经验教训:要改变我应该做的情况:

git mv "final report.odt" temp.odt
git mv temp.odt "Final Report.odt"

Again, that wasn't the problem for the OP here; and wouldn't affect a Linux user, as his posts shows he clearly is. I'm just including it for others who may have this problem in Windows git and stumble onto this question.

同样,这不是 OP 的问题;并且不会影响 Linux 用户,因为他的帖子清楚地表明他是。我只是为其他可能在 Windows git 中遇到此问题并偶然发现此问题的人提供它。

回答by Lord Nighton

If your file idea/workspace.xmlis added to .gitignore (or its parent folder) just addit manually to git version control. Also you can add it using TortoiseGit. After the next push you will see, that your problem is solved.

如果您的文件idea/workspace.xml被添加到 .gitignore (或其父文件夹),只需add手动将其添加到 git 版本控制。您也可以使用 TortoiseGit 添加它。下一次推送后,您将看到,您的问题已解决。

Add to git versioning using TortoiseGit

使用 TortoiseGit 添加到 git 版本控制

回答by Billal Begueradj

Personally I stumbled on a similar error message in this scenario:

我个人在这种情况下偶然发现了类似的错误消息:

I created a folder that has been empty, so naturally as long as it is empty, typing git add *will not take this empty folder in consideration. So when I tried to run git rm -r *or simply git rm my_empty_folder/ -r,I got that error message.

我创建了一个一直是的文件夹,所以自然只要是空的,打字git add *就不会考虑这个空文件夹。因此,当我尝试运行git rm -r *或简单地使用 git 时,rm my_empty_folder/ -r,我收到了该错误消息。

The solution is to simply remove it without git: rm -r my_empty_folder/or create a data file within this folder and then add it (git add my_no_long_empty_folder)

解决方法是简单地删除它而不使用 git:rm -r my_empty_folder/或者在这个文件夹中创建一个数据文件然后添加它 ( git add my_no_long_empty_folder)

回答by Mykola

Such steps helped me:

这些步骤帮助了我:

  1. git add .
  2. git stash
  1. git 添加。
  2. 混帐

回答by Will Strohl

In my instance, there was something completely odd that I'm not sure what the cause was. An entire folder was committed previously. I could see it in Git, Windows Explorer, and GitHub, but any changes I made to the folder itself and the files in it were ignored. Using git check-ignoreto see what was ignoring it, and attempting to remove it using git rm --cachedhad no impact. The changes were not able to be staged.

就我而言,有一些完全奇怪的事情,我不确定原因是什么。之前提交了整个文件夹。我可以在 Git、Windows 资源管理器和 GitHub 中看到它,但是我对文件夹本身和其中的文件所做的任何更改都被忽略了。使用git check-ignore查看忽略它的内容,并尝试使用删除它git rm --cached没有影响。更改无法进行。

I fixed it by:

我通过以下方式修复它:

  1. Making a copy of the folder and files in another location.
  2. I deleted the original that was getting ignored somehow.
  3. Commit and push this update.
  4. Finally, I added the files and folder back and git was seeing and reacting to it as expected again.
  5. Stage and commit this, and you're good to go! :)
  1. 在另一个位置制作文件夹和文件的副本。
  2. 我删除了以某种方式被忽略的原件。
  3. 提交并推送此更新。
  4. 最后,我重新添加了文件和文件夹,git 再次按预期看到并对其做出反应。
  5. 登台并提交,你就可以开始了!:)

回答by user4551034

Move temporarily .gitignore to .gitignore.bck

暂时将 .gitignore 移动到 .gitignore.bck