git rm - fatal: pathspec did not match any files
Asked Answered
C

13

141

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.

Curvet answered 23/8, 2014 at 4:0 Comment(0)
U
135

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.

Ullage answered 23/8, 2014 at 4:40 Comment(10)
Hey, i'm with the same problem but when typing the command at step 1, i get an error: fatal: bad revision ' --prune-empty'. Any clue?Gavage
@Gavage sorry, missed this comment. You can run it without that flag. Pruning would have removed any empty commit object.Ullage
Shouldn't this line git add .gitignore && commit -m "ignore rule for photos" be git add .gitignore && git commit -m "ignore rule for photos"Phosphorism
@Phosphorism yes, should've been that, have corrected it now :)Ullage
On windows I right-clicked on the source-dir in an explorer window, selected 'use git bash here' and ran the command. Didn't work for me with git cmd-windows, it had to be git bash.Petticoat
This is an insanely awesome answer. Fixed an issue I've been having with my repo for several years. Thanks!Diella
I followed up this answer to split out a dir from a giant repo. Thanks.Jarred
I aslo needed "git reflog expire --expire=now --all && git gc --prune=now --aggressive" to actually reduce the size of .git folderOctennial
I'm trying to do something similar with my repository, but I get an error "fatal: bad revision 'rm'". Am I supposed to enter all three of those lines as a single command? That's what I'm currently doing.Josi
@AnshulGoyal it is heavily dissuaded (even by its manpage itself) from using git-filter-branch due to too many things which could for example work on one system, but fail on another and harm the repository badly (e.g. encoding issues). Instead, use git-filter-repo: github.com/newren/git-filter-repoUnpleasantness
B
37

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.

Briticism answered 20/10, 2016 at 11:13 Comment(2)
this will not do what the OP wants, which is to remove files which are present in previous commits. You need to use git filter-branch as mentioned elsewhere.Synergetic
This does not work.Father
L
12

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.

Lepidus answered 4/6, 2016 at 3:1 Comment(2)
... i always thought git was harder than it should be this proves it. There is no way anyone can even remember the existence of most git commands let alone their flags and quirks. Git model makes sense, and it works until you follow simple flow, and as soon as you get stuck, you get stuck hard.Schwing
This is the solution that works for me, I needed the --ignore-unmatchHepatitis
A
7

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.

Appellee answered 18/9, 2019 at 7:18 Comment(3)
Made my day thank you!Capriola
what does 'cat -- --all' do at the end of the command?Nevski
cat command is used to console output.Appellee
M
6

using this worked for me

git rm -f --cached <filename>
Mccloskey answered 12/1, 2020 at 17:49 Comment(0)
S
3

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

Spectatress answered 18/6, 2021 at 13:15 Comment(0)
C
1
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.

Corrugation answered 28/5, 2018 at 15:36 Comment(0)
S
1

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>
Schopenhauer answered 4/6, 2018 at 13:35 Comment(0)
T
0

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
Tubate answered 29/4, 2021 at 10:39 Comment(0)
B
0

I got the same error while I wanted to delete cached node_modules dir.

instead of
git rm -r -f --cached node_modules I had to use
git rm -r -f --cached **/node_modules, because the dir was not part of the root dir.

Baxie answered 14/10, 2022 at 7:25 Comment(0)
B
0

I had this error when I aborted an upload which was taking too long, to fix it I renamed the file, commit, renamed it back, commit.

Buss answered 24/5, 2023 at 8:35 Comment(0)
D
0

I had a similar issue with the following error message (Google brought me here):

error: pathspec 'elements (conflicted copy 2013-08-06)' did not match any file(s) known to git

I tried all of the solutions described here, but none worked for me. I had two files that were simply not there anymore, but git was still showing me the files and it was impossible to remove them. They were created thanks to a conflict with Dropbox.

Steps that worked for me:

  1. git stash

This resulted in the two files existing again in the folder

  1. rename the files to something else

Git will now show the renamed files as untracked, but the old files still as deleted

Changes not staged for commit: (use "git add/rm ..." to update what will be committed) (use "git restore ..." to discard changes in working directory)
deleted: "elements (conflicted copy 2013-08-06)"
deleted: "layout (conflicted copy 2013-08-06)"

Untracked files: (use "git add ..." to include in what will be committed)
elements_dupe
layout_dupe

  1. git add -A

Suddenly, git status displayed, that the previous files were renamed

Changes to be committed: (use "git restore --staged ..." to unstage)

renamed: "elements (conflicted copy 2013-08-06)" -> elements_dupe renamed: "layout (conflicted copy 2013-08-06)" -> layout_dupe

  1. git commit -m 'renamed duplicate files'

Afterwards, it was very easy to remove them by simply deleting the files and commit again.

Deign answered 16/6, 2023 at 9:43 Comment(0)
V
-1

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.

Voltage answered 13/11, 2019 at 23:20 Comment(1)
Sorry! I must've misread the question. I thought that's what the goal wasVoltage

© 2022 - 2024 — McMap. All rights reserved.