【问题标题】:How to remove these non existing files from Git?如何从 Git 中删除这些不存在的文件?
【发布时间】:2022-01-10 10:34:53
【问题描述】:

MS Word 确实在打开文件时自动创建了这些文件。我不小心提交了这些文件。关闭 MS Word 后,文件已自动删除。现在 Git 仍然显示文件,我无法删除、添加或签出它们。如何从存储库中删除这些文件?

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)
  
    deleted:    NonProject/GameObjectSetupInstructions/RaceScene/~$VFX.docx
    deleted:    NonProject/GameObjectSetupInstructions/RaceScene/~$ceSceneLighting.docx
    modified:   NonProject/GameObjectSetupInstructions/RaceScene/~$ceSceneSetup.docx
        
        
        
$ git checkout NonProject/GameObjectSetupInstructions/RaceScene/~$VFX.docx
error: pathspec 'NonProject/GameObjectSetupInstructions/RaceScene/~.docx' did not match any file(s) known to git

$ git rm --cached NonProject/GameObjectSetupInstructions/RaceScene/~$VFX.docx
fatal: pathspec 'NonProject/GameObjectSetupInstructions/RaceScene/~.docx' did not match any files

$ git rm NonProject/GameObjectSetupInstructions/RaceScene/~$VFX.docx
fatal: pathspec 'NonProject/GameObjectSetupInstructions/RaceScene/~.docx' did not match any files

【问题讨论】:

  • 试试git add 'NonProject/GameObjectSetupInstructions/RaceScene/~$VFX.docx'?
  • 成功了!我总是尝试使用“”而不是“”。我认为它是等价的......无论如何,非常感谢。
  • @MasterMax 波浪号 ~ 可能被解释为您的主目录。试试cd ~cd ~/Documents

标签: git


【解决方案1】:

在 bash 中,当您在命令中使用变量时,它会以 $ 为前缀。因此,当您键入时:

git rm NonProject/GameObjectSetupInstructions/RaceScene/~$VFX.docx

Bash 尝试用变量 VFX 替换 $VFX。如果找不到,则使用空字符串,如您在错误消息中所见:

fatal: pathspec 'NonProject/GameObjectSetupInstructions/RaceScene/~.docx' did not match any files

请注意,$VFX 已消失。

解决办法是用单引号将路径括起来,这样可以防止变量替换:

git rm 'NonProject/GameObjectSetupInstructions/RaceScene/~$VFX.docx'

【讨论】:

  • 在使用双引号的时候不会发生替换吗?
  • @evolutionxbox:对于 bash,是的。不同的 shell 有不同的规则,所以它可能会变得混乱,但这就是单引号起作用的原因。
  • 我先尝试了双引号,但它在我的 Git shell 中不起作用。单引号有效。
猜你喜欢
  • 2011-01-04
  • 2013-11-12
  • 2012-07-09
  • 1970-01-01
  • 2012-02-23
  • 2010-09-11
  • 1970-01-01
相关资源
最近更新 更多