git rm - fatal:pathspec没有匹配任何文件 [英] git rm - fatal: pathspec did not match any files

查看:589
本文介绍了git rm - fatal:pathspec没有匹配任何文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我偶然添加了超过9000张照片到我的项目文件夹。并承诺他们。然后从磁盘中删除它们。承诺。

现在我尝试将更改推送到git服务器。但它花费太长时间并尝试发送12 Gb的数据。

我检查了磁盘上的文件大小,发现真正的 .git 文件夹需要12 Gb。



如何从那里删除照片?我尝试了 git rm ,但失败:

 ❯git rm public /照片
fatal:pathspec'public / photos'与任何文件都不匹配

将它们从磁盘中删除,但它们仍在 .git 文件夹中。



我试图将 public / photos 添加到 .gitignore

  public / photos / 
* .zip

但没有结果。
当然,当我在我的项目中没有太多垃圾照片的时候,我可以硬重置磁头。但自那时以来,我犯了许多次错误,并在代码上做了很多改变。

解决方案

在你的情况下,使用 git filter-branch 而不是 git rm p>

git rm 会删除这些文件,因为它们不会被git跟踪,但不会删除旧的提交对象与这些图像相对应,因此您仍然会推迟前面提交的对应于12GB图像的提交。



另一方面,git filter-branch 也可以从之前的所有提交中删除这些文件,因此不需要推送任何文件。


  1. 使用命令

      git filter-branch --force --index-filter \ 
    'git rm -r --cached --ignore -unmatch public / photos'\
    --prune-empty --tag-name-filter cat - - -all


  2. 过滤器分支完成后,验证没有意外的文件丢失。

  3. 现在添加一个.gitignore规则

      echo public / photos>> .gitignore 
    git add .gitignore&& git commit -m忽略照片规则




  4.   git push -f原始分支


查看这个这个获得进一步的帮助。为了更安全一些,我建议你在继续使用这些指令之前在你的系统上创建一个repo的备份副本。



至于你的严重错误信息,它发生是因为你已经用 git rm 来解开它们,所以git抱怨,因为它不能删除它没有跟踪的文件。阅读关于此的更多信息


I added over 9000 photos by accident to my project folder. And committed them. Then deleted them from disk. Committed.

Now I try to push changes to git server. But it takes too long and tries to send 12 Gb of data.

I checked files size on disk and see that really .git folder takes 12 Gb.

How to delete photos from there? I tried git rm, but fails:

❯ git rm public/photos
fatal: pathspec 'public/photos' did not match any files

Because I allready deleted them from disk, but they are still in .git folder.

I tried to add public/photos to .gitignore:

public/photos/
*.zip

But no result. Of course I could hard reset head to moment when I did not have so many junk photos in my project. But since that time i committed many times and made a lot changes in code.

解决方案

In your case, use git filter-branch instead of git rm.

git rm will delete the files in the sense that they will not be tracked by git anymore, but that does not remove the old commit objects corresponding to those images, and so you will still be stuck with pushing the earlier commits which correspond to 12GB of images.

The git filter-branch, on the other hand, can remove those files from all the previous commits as well, thus doing away with the need to push any of them.

  1. Use the command

    git filter-branch --force --index-filter \
      'git rm -r --cached --ignore-unmatch public/photos' \
      --prune-empty --tag-name-filter cat -- --all
    

  2. After the filter branch is complete, verify that no unintended file was lost.

  3. Now add a .gitignore rule

    echo public/photos >> .gitignore
    git add .gitignore && git commit -m "ignore rule for photos"
    

  4. Now do a push

    git push -f origin branch
    

Check this, this and this for further help. Just to be on the safer side, I would suggest you create a backup copy of the repo on your system before going ahead with these instructions.

As for your orignial error message, it is happening because you already untracked them using git rm, and hence git is complaining because it can't remove a file it isn't tracking. Read more about this here.

这篇关于git rm - fatal:pathspec没有匹配任何文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆