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 to long and tries to send 12 Gb of data.
I checked files size on disk and see that really
❯ git rm public/photos
fatal: pathspec 'public/photos' did not match any files
hard reset head
In your case, use
git filter-branch instead of
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 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.
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
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.