137

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.

David Parks
  • 30,789
  • 47
  • 185
  • 328
Maxim Yefremov
  • 13,671
  • 27
  • 117
  • 166

13 Answers13

127

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.

Community
  • 1
  • 1
Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
  • 2
    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? – kevin Oct 16 '14 at 21:48
  • @kevin sorry, missed this comment. You can run it without that flag. Pruning would have removed any empty commit object. – Anshul Goyal Feb 02 '15 at 04:13
  • 2
    Shouldn't this line `git add .gitignore && commit -m "ignore rule for photos"` be `git add .gitignore && git commit -m "ignore rule for photos"` – Clone Apr 01 '15 at 08:47
  • @Clone yes, should've been that, have corrected it now :) – Anshul Goyal Apr 01 '15 at 09:52
  • 1
    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. – Morten Nørgaard Feb 27 '17 at 13:37
  • 1
    This is an insanely awesome answer. Fixed an issue I've been having with my repo for several years. Thanks! – Moshe Sep 18 '17 at 21:34
  • I followed up this answer to split out a dir from a giant repo. Thanks. – paulz Jul 15 '20 at 01:30
  • I aslo needed "git reflog expire --expire=now --all && git gc --prune=now --aggressive" to actually reduce the size of .git folder – Istvan Heckl May 06 '21 at 10:05
  • 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. – Adam Ryason Apr 21 '22 at 17:27
  • @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: https://github.com/newren/git-filter-repo – user7427029 Oct 14 '22 at 10:39
36

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.

Bharti Rawat
  • 1,949
  • 21
  • 32
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.

Will
  • 24,082
  • 14
  • 97
  • 108
klmlfl
  • 431
  • 4
  • 5
  • 15
    ... 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. – Muhammad Umer Aug 17 '16 at 15:05
  • 2
    This is the solution that works for me, I needed the `--ignore-unmatch` – guhur May 28 '18 at 19:42
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.

Kiran Maniya
  • 8,453
  • 9
  • 58
  • 81
6

using this worked for me

git rm -f --cached <filename>
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

Vakho Jgenti
  • 369
  • 3
  • 9
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.

user1767316
  • 3,276
  • 3
  • 37
  • 46
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>
Daniel Puiu
  • 962
  • 6
  • 21
  • 29
Ustin
  • 568
  • 6
  • 19
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
Sérgio
  • 6,966
  • 1
  • 48
  • 53
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.

Alex Szücs
  • 561
  • 5
  • 12
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.

7RedBits.com
  • 454
  • 6
  • 6
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.

gorkem
  • 500
  • 6
  • 8
-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.

ccsinsf
  • 3
  • 3