Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git rm - fatal: pathspec did not match any files

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.

like image 512
Maxim Yefremov Avatar asked Aug 23 '14 04:08

Maxim Yefremov


People also ask

What is the difference between Git git RM and pathspec?

git rm normally outputs one line (in the form of an rm command) for each file removed. This option suppresses that output. Pathspec is passed in <file> instead of commandline args. If <file> is exactly - then standard input is used. Pathspec elements are separated by LF or CR/LF.

Why do I get fatal pathspec did not match any files?

If you are trying to add a file to staging area using git command and you get the fatal pathspec did not match any files error, well reason could be one of the below, The file that you are trying to add to the staging area does not exist. You have misspelled the filename. You are in the wrong branch.

What is pathspec'develop'error in Git?

Git - Error: pathspec 'develop' did not match any file (s) known to git. (Example) Git - Error: pathspec 'develop' did not match any file (s) known to git.

What does gitgit RM do to the files?

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.


2 Answers

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.

like image 137
Anshul Goyal Avatar answered Oct 23 '22 05:10

Anshul Goyal


A very simple answer is.

Step 1:

Firstly add your untracked files to which you want to delete:

using git add . or git add <filename>.

Step 2:

Then delete them easily using command git rm -f <filename> here rm=remove and -f=forcely.

like image 41
Bharti Rawat Avatar answered Oct 23 '22 07:10

Bharti Rawat