git rm - fatal: pathspec did not match any files

GitGit Filter-Branch

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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.

Solution 3 - Git

Step 1

Add the file name(s) to your .gitignore file.

Step 2

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

Step 3

git push -f origin branch

A big thank you to @mu.

Solution 4 - Git

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 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.

Solution 5 - Git

using this worked for me

git rm -f --cached <filename>

Solution 6 - Git

Sometimes it's happens when you are not tracking your file add your file with git add untracked file name, after this simply do git rm -r file name

Solution 7 - Git

git stash 

did the job, It restored the files that I had deleted using rm instead of git rm.

I did first a checkout of the last hash, but I do not believe it is required.

Solution 8 - Git

This chains work in my case:

  1. git rm -r WebApplication/packages

There was a confirmation git-dialog. You should choose "y" option.

  1. git commit -m "blabla"
  2. git push -f origin <ur_branch>

Solution 9 - Git

Just for reference, I noticed that I had some files with permissions 000, maybe they were created with sudo , I don't know, but after changing your permissions to 644 (I needed sudo for this operation), the problem was solved

sudo chmod 644 vendor/symfony/yaml

and the commit result :

diff --git a/vendor/symfony/yaml b/vendor/symfony/yaml
deleted file mode 160000
index 212a27b7..00000000
--- a/vendor/symfony/yaml
+++ /dev/null
@@ -1 +0,0 @@
-Subproject commit 212a27b731e5bfb735679d1ffaac82bd6a1dc996
diff --git a/vendor/symfony/yaml b/vendor/symfony/yaml
new file mode 100644
index 00000000..20a0b9d0
--- /dev/null
+++ b/vendor/symfony/yaml
@@ -0,0 +1 @@
+Subproject commit 212a27b731e5bfb735679d1ffaac82bd6a1dc996

Solution 10 - Git

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMaxim YefremovView Question on Stackoverflow
Solution 1 - GitAnshul GoyalView Answer on Stackoverflow
Solution 2 - GitBharti RawatView Answer on Stackoverflow
Solution 3 - GitklmlflView Answer on Stackoverflow
Solution 4 - GitKiran ManiyaView Answer on Stackoverflow
Solution 5 - GitAndreia OliveiraView Answer on Stackoverflow
Solution 6 - GitVakho JgentiView Answer on Stackoverflow
Solution 7 - Gituser1767316View Answer on Stackoverflow
Solution 8 - GitUstinView Answer on Stackoverflow
Solution 9 - GitSérgioView Answer on Stackoverflow
Solution 10 - GitccsinsfView Answer on Stackoverflow