git rm - 致命:pathspec 不匹配任何文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25458306/
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
git rm - fatal: pathspec did not match any files
提问by Maxim Yefremov
I added over 9000 photos by accident to my project folder. And committed them. Then deleted them from disk. Committed.
我不小心将 9000 多张照片添加到我的项目文件夹中。并犯了他们。然后从磁盘中删除它们。坚定的。
Now I try to push changes to git server. But it takes too long and tries to send 12 Gb of data.
现在我尝试将更改推送到 git 服务器。但它需要太长时间并尝试发送 12 Gb 的数据。
I checked files size on disk and see that really .git
folder takes 12 Gb.
我检查了磁盘上的文件大小,发现真正的.git
文件夹需要 12 Gb。
How to delete photosfrom there? I tried git rm
, but fails:
如何从那里删除照片?我试过了git rm
,但失败了:
? 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.
因为我已经从磁盘中删除了它们,但它们仍在.git
文件夹中。
I tried to add public/photos
to .gitignore
:
我试图添加public/photos
到.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.
但没有结果。当然hard reset head
,当我的项目中没有这么多垃圾照片时,我可以。但是从那时起我多次提交并在代码中进行了很多更改。
回答by u1860929
In your case, use git filter-branch
instead of git rm
.
在您的情况下,使用git filter-branch
代替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.
git rm
将删除文件,因为它们将不再被 git 跟踪,但这不会删除与这些图像对应的旧提交对象,因此您仍然会坚持推送对应于 12GB 图像的较早提交。
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.
的git filter-branch
,而另一方面,可以从所有先前提交删除这些文件,以及,因而消除了需要推动任何人。
Use the command
git filter-branch --force --index-filter \ 'git rm -r --cached --ignore-unmatch public/photos' \ --prune-empty --tag-name-filter cat -- --all
After the filter branch is complete, verify that no unintended file was lost.
Now add a .gitignore rule
echo public/photos >> .gitignore git add .gitignore && git commit -m "ignore rule for photos"
Now do a push
git push -f origin branch
使用命令
git filter-branch --force --index-filter \ 'git rm -r --cached --ignore-unmatch public/photos' \ --prune-empty --tag-name-filter cat -- --all
过滤器分支完成后,验证没有意外文件丢失。
现在添加一个 .gitignore 规则
echo public/photos >> .gitignore git add .gitignore && git commit -m "ignore rule for photos"
现在推
git push -f origin branch
Check this, thisand thisfor 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.
检查这个,这个和这个以获得进一步的帮助。为了更安全,我建议您在继续执行这些说明之前在系统上创建一个 repo 的备份副本。
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
,因此 git 抱怨,因为它无法删除它没有跟踪的文件。在此处阅读更多相关信息。
回答by Bharti Rawat
A very simple answer is.
一个非常简单的答案是。
Step 1:
第1步:
Firstly add your untracked files to which you want to delete:
首先添加要删除的未跟踪文件:
using git add .
or git add <filename>
.
使用git add .
或git add <filename>
。
Step 2:
第2步:
Then delete them easily using command git rm -f <filename>
here rm=remove and -f=forcely.
然后git rm -f <filename>
在这里使用rm=remove 和-f=forcely命令轻松删除它们。
回答by klmlfl
Step 1
第1步
Add the file name(s) to your .gitignore
file.
将文件名添加到您的.gitignore
文件中。
Step 2
第2步
git filter-branch --force --index-filter \
'git rm -r --cached --ignore-unmatch YOURFILE' \
--prune-empty --tag-name-filter cat -- --all
Step 3
第 3 步
git push -f origin branch
A big thank you to @mu.
非常感谢@mu。
回答by Andreia Oliveira
using this worked for me
使用这个对我有用
git rm -f --cached <filename>
回答by Ustin
This chains work in my case:
这个链在我的情况下工作:
git rm -r WebApplication/packages
git rm -r WebApplication/packages
There was a confirmation git-dialog. You should choose "y" option.
有一个确认 git-dialog。您应该选择“y”选项。
git commit -m "blabla"
git push -f origin <ur_branch>
git commit -m "blabla"
git push -f origin <ur_branch>
回答by Kiran Maniya
To remove the tracked and old committed file from git you can use the below command. Here in my case, I want to untrack and remove all the file from dist
directory.
要从 git 中删除已跟踪和旧提交的文件,您可以使用以下命令。就我而言,我想取消跟踪并从dist
目录中删除所有文件。
git filter-branch --force --index-filter 'git rm -r --cached --ignore-unmatch dist' --tag-name-filter cat -- --all
Then, you need to add it into your .gitignore
so it won't be tracked further.
然后,您需要将其添加到您的文件中,.gitignore
以免被进一步跟踪。
回答by user1767316
git stash
did the job,
It restored the files that I had deleted using rm
instead of git rm
.
完成了这项工作,它恢复了我使用rm
而不是git rm
.
I did first a checkout of the last hash, but I do not believe it is required.
我首先检查了最后一个哈希值,但我认为这不是必需的。
回答by ccsinsf
I had a duplicate directory (~web/web) and it removed the nested duplicate when I ran rm -rf web
while inside the first web folder.
我有一个重复的目录 (~web/web),当我rm -rf web
在第一个 web 文件夹中运行时,它删除了嵌套的重复项。